Can the content used in Sitecore PXM / APS be controlled by the workflow even when editing in InDesign?

Background

I use content that is controlled by the workflow in the Sitecore Print Experience Manager (PXM *) project. One of the great features of PXM is the ability to save content changes to Sitecore when viewing an InDesign stream.

Problem

Unfortunately, when you save a content item to Sitecore, it saves data without involving the workflow in the item.

Question

Is there any way to enable a workflow with the InQuery Sitecore PXM connector?

* formerly known as Adaptive Print Studio or APS

+4
source share
1 answer

PXM . , Mark Demeny Sitecore, . , .

item:

Sitecore: , InDesign , , , , .

SaveProcessor.cs

public class SaveProcessor
{
    private Template _StandardTemplate;
    private const string PRINT_STUDIO_SITE_NAME = "printstudio";
    private static readonly List<Guid> _ActiveItemIds = new List<Guid>();

    public void OnItemSaving(object sender, EventArgs args)
    {
        // Only intercept updates from PXM
        if (Context.Site.Name.Equals(PRINT_STUDIO_SITE_NAME))
        {
            var sitecoreEventArgs = args as SitecoreEventArgs;
            var updatedItem = sitecoreEventArgs?.Parameters[0] as Item;

            if (updatedItem != null)
            {
                // If we're already working with this item, allow this save to continue normally (prevents infinite recursion)
                if (!_ActiveItemIds.Contains(updatedItem.ID.Guid))
                {
                    var originalItem = Context.Database.GetItem(updatedItem.ID);
                    if (originalItem != null)
                    {
                        var workflow = Context.Database.WorkflowProvider.GetWorkflow(originalItem);
                        var workflowState = workflow?.GetState(originalItem);

                        // If the current item is not in workflow, or it is in workflow but the current state is not final, allow the save to continue normally
                        if (workflowState != null && workflowState.FinalState)
                        {
                            var differences = new Dictionary<string, string>();
                            foreach (Field field in updatedItem.Fields)
                            {
                                var updatedItemField = updatedItem.Fields[field.ID];
                                var originalItemField = originalItem.Fields[field.ID];

                                // Find all the differences that are not standard fields
                                if (updatedItemField != null &&
                                    !IsStandardField(updatedItemField) &&
                                    originalItemField != null &&
                                    !updatedItemField.Value.Equals(originalItemField.Value))
                                {
                                    differences.Add(field.Name, updatedItemField.Value);
                                }
                            }

                            // If there are no differences, allow the save to continue normally
                            if (differences.Count > 0)
                            {
                                // Add this item ID to the list of currently-processing item IDs
                                _ActiveItemIds.Add(updatedItem.ID.Guid);

                                try
                                {
                                    originalItem.Editing.BeginEdit();
                                    var newVersion = originalItem.Versions.AddVersion();
                                    newVersion.Editing.BeginEdit();
                                    foreach (var difference in differences)
                                    {
                                        newVersion[difference.Key] = difference.Value;
                                    }
                                    newVersion.Editing.EndEdit();
                                    originalItem.Editing.EndEdit();
                                }
                                finally
                                {
                                    // Remove this item ID from the list of currently-processing item IDs
                                    _ActiveItemIds.Remove(updatedItem.ID.Guid);
                                }

                                sitecoreEventArgs.Result.Cancel = true;
                            }
                        }
                    }
                }
            }
        }
    }

    public bool IsStandardField(Field field)
    {
        if (_StandardTemplate == null)
            _StandardTemplate = TemplateManager.GetTemplate(Sitecore.Configuration.Settings.DefaultBaseTemplate, field.Database);

        return _StandardTemplate.ContainsField(field.ID);
    }
}

web.config, .

App_Config\Include\SaveProcessor.config

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
    <events>
        <event name="item:saving">
            <handler type="TestPxm.Pxm.SaveProcessor,TestPxm" method="OnItemSaving" patch:before="handler[@type='Sitecore.Tasks.ItemEventHandler, Sitecore.Kernel']"/>
        </event>
    </events>
</sitecore>
</configuration>
+3

Source: https://habr.com/ru/post/1618959/


All Articles