0

I have a custom workflow action that copies the content from the current item to a new item. The user is prompted to select a location in the content tree to save the new item. The new item is created just fine but when you click on the new item in the content tree, you get a 500 error dialog. The error message in the logs is:

Multiple controls with the same ID 'FContent8C06B64892A2439688C27B85FB8F73CA' 
were found. FindControl requires that controls have unique IDs.

If you refresh the page, the error goes away and never comes back.

We dug into this and found that the control ID referenced is the Control Panel Content pane. When the new item is created, it is selected in the Content Tree. However, the Content Pane still displays the original item. We believe this is the issue but we need help figuring out how to get the content pane to update to display the new item.

This is a Sitecore 8.2 Update-6 project. The custom workflow action code is below. Your help is much appreciated!

public class MapKSWContentToPageType
{
    private SearchHelperExtensions searchHelpers = new SearchHelperExtensions();
    private Sitecore.Data.Database database = Sitecore.Configuration.Factory.GetDatabase("master");

    public void Process(WorkflowPipelineArgs args)
    {
        try
        {
            var workflowItem = args.DataItem; // The item in the workflow
            var branchTemplateId = WebUtil.ParseUrlParameters(args.ProcessorItem.InnerItem["parameters"])["branchtemplateid"];
            var fieldsMapping = WebUtil.ParseUrlParameters(args.ProcessorItem.InnerItem["parameters"])["fields"];

            ClientPipelineArgs clientArgs = new ClientPipelineArgs();
            clientArgs.CustomData.Add("kswContentItemId", workflowItem.ID.ToString());
            clientArgs.CustomData.Add("branchTemplateId", branchTemplateId);
            clientArgs.CustomData.Add("fieldsMapping", fieldsMapping);

            Context.ClientPage.Start(this, "GetNewItemLocation", clientArgs);
        }
        catch (Exception ex)
        {
            //An error occurred, set the item workflow to Error
            Sitecore.Diagnostics.Log.Error("KSW create page workflow action error.", ex, this);
        }
    }

    public void GetNewItemLocation(ClientPipelineArgs clientArgs)
    {
        if (!clientArgs.IsPostBack)
        {
            SheerResponse.ShowModalDialog(new ModalDialogOptions("/sitecore/client/Applications/Dialogs/InsertSitecoreItemViaTreeDialog")
            {
                Header = "Select the location for the new item",
                Maximizable = true,
                Width = "700",
                Height = "700",
                Response = true,
                ForceDialogSize = true
            });

            clientArgs.WaitForPostBack();
        }
        else
        {
            if (clientArgs.HasResult)
            {
                Sitecore.Data.Items.Item item = database.GetItem(clientArgs.Result);

                if (item != null)
                {
                    clientArgs.CustomData.Add("newItemLocationId", item.ID.ToString());

                    string newItemLocationId = clientArgs.CustomData["newItemLocationId"].ToString();
                    string kswContentItemId = (string)clientArgs.CustomData["kswContentItemId"];
                    string branchTemplateId = (string)clientArgs.CustomData["branchTemplateId"];
                    var fieldsMappingParameter = (string)clientArgs.CustomData["fieldsMapping"];

                    Dictionary<string, string> fieldMappings = GetFieldMappings(fieldsMappingParameter);

                    if (!string.IsNullOrEmpty(newItemLocationId) && !string.IsNullOrEmpty(kswContentItemId) && !string.IsNullOrEmpty(branchTemplateId))
                    {
                        //Map the Page Type
                        CreatePageTypeItem(kswContentItemId, newItemLocationId, branchTemplateId, fieldMappings);
                    }
                }
                else
                {
                    SheerResponse.Alert(string.Format("The item '{0}' was not found.", clientArgs.Result));
                }
            }
        }
    }

    public void CreatePageTypeItem(string kswContentItemId, string newItemLocationId, 
        string branchTemplateId, Dictionary<string, string> fieldMappings)
    {
        try
        {
            using (new SecurityDisabler())
            {
                BranchItem branchTemplate = database.GetItem(branchTemplateId);
                Sitecore.Data.Items.Item targetLocation = database.GetItem(newItemLocationId);
                var kswContentItem = database.GetItem(kswContentItemId).As<KswContentItem>();

                if (branchTemplate != null && targetLocation != null && kswContentItem != null)
                {
                    var pageType = targetLocation.Add(kswContentItem.DisplayName, branchTemplate);

                    pageType.Editing.BeginEdit();

                    foreach(var fieldMapping in fieldMappings)
                    {
                        pageType.Fields[fieldMapping.Key].Value = kswContentItem.InnerItem.Fields[fieldMapping.Value].Value;
                    }

                    MultilistField hadKswConnector = pageType.Fields["KSW Content Items"];
                    hadKswConnector.Add(kswContentItemId);

                    //Populate Taxonomy
                    if (kswContentItem.KswServiceLines.Count() > 0)
                    {
                        foreach (var serviceLine in kswContentItem.KswServiceLines)
                        {
                            KswServiceLine kswServiceLine = database.GetItem(serviceLine).As<KswServiceLine>();

                            if (kswServiceLine != null)
                            {
                                 var relatedTaxonomyTerms = kswServiceLine.RelatedTaxonomy.TargetItems.ToList();

                                if (relatedTaxonomyTerms != null && relatedTaxonomyTerms.Count() > 0)
                                {
                                    MultilistField pageTypeTaxonomy = pageType.Fields["Taxonomy Terms"];
                                    if (pageTypeTaxonomy != null)
                                    {
                                        foreach (var relatedTaxonomyTerm in relatedTaxonomyTerms)
                                        {
                                            if (relatedTaxonomyTerm != null && pageTypeTaxonomy.TargetIDs != null)
                                            {
                                                if (!pageTypeTaxonomy.TargetIDs.Contains(relatedTaxonomyTerm.Id))
                                                {
                                                    pageTypeTaxonomy.Add(relatedTaxonomyTerm.Id.ToString());
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }

                    pageType.Editing.EndEdit();

                    //Update Search Index
                    var newItem = (SitecoreIndexableItem)pageType;
                    string searchIndex = searchHelpers.GetSearchIndexByEnvironment();
                    ContentSearchManager.GetIndex(searchIndex).Refresh(newItem);
                }
            }
        }
        catch (Exception ex)
        {
            string errMessage = string.Format("KSW CREATE PAGE TYPE MAPPING ERROR: {0} {1} {2}SOURCE: {3}", ex.Message, ex.StackTrace, Environment.NewLine, kswContentItemId);
            Sitecore.Diagnostics.Log.Error(errMessage, ex, this);
        }
    }

    protected Dictionary<string, string> GetFieldMappings(string fieldsMappingParameter)
    {
        var result = new Dictionary<string, string>();
        try
        {

            var array = JArray.Parse(fieldsMappingParameter);
            foreach (JObject ob in array.Children<JObject>())
            {
                foreach (JProperty property in ob.Properties())
                {
                    string name = property.Name;
                    string value = (string)property.Value;
                    if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(value))
                    {
                        result.Add(name.Trim(), value.Trim());
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Sitecore.Diagnostics.Log.Error("Error parsing field mappings.", ex, this);
        }

        return result;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.