1
votes

I'd like to update the value of placeholder(PH) key assigned into each page item.

The problem is I changed the value of PH key in master template (actually combined two templates to make only one template) and a number of pages should be updated with new assigned PH key.

How to update placeholder key without clicking each item and changing the value in presentation? If I do like this, it takes a lot of time.

What I want to do in program is:

  1. Set initial path (/sitecore/home/robot/)
  2. Check each item (with each item's sub-item) in initial path
  3. Retrieve each item's assigned controls in presentation
  4. If there is "Breadcrumbs" control with "breadcrumbs" key name
  5. Then, change the value to "/template/dynamic/breadcrumbs"
  6. Do until it retrives all items in the initial path
1
What have you got so far? - Martin Davies
If you've made use of template standard values, you might actually be able to do a fair bit of this task by simply updating the placeholder settings on a few templates' standard values items. - Matthew Dresser
Yes, it makes a sense because I used insert option. Otherwise, each page has different additional controls which are not included in standard values. - Jay

1 Answers

1
votes

See the code below. What it does, it gets rendering references for the selected items, checks their placeholders and rendering names and updates xml value of the __Renderings field of selected item, based on the unique id of selected renderings. Then it fires same code for all descendants recursively.

This code

  • does not update placeholders for components which are inherited from __Standard Values
  • does not publish changed items automatically.
  • is case sensitive
  • requires that user has write access for the items that you want to change
public void Start()
{
    string initialPath = "/sitecore/home/robot";
    Item root = Database.GetDatabase("master").GetItem(initialPath);
    UpdatePlaceholderName(root, "Breadcrumbs", "breadcrumbs", "/template/dynamic/breadcrumbs");
}

private void UpdatePlaceholderName(Item item, string componentName, string placeholderName, string newPlaceholderName)
{
    if (item != null)
    {
        List<RenderingReference> renderings = item.Visualization.GetRenderings(Sitecore.Context.Device, false)
            .Where(r => r.Placeholder == placeholderName && r.RenderingItem.Name == componentName).ToList();

        if (renderings.Any())
        {
            string renderingsXml = item["__Renderings"];
            item.Editing.BeginEdit();
            foreach (RenderingReference rendering in renderings)
            {
                string[] strings = renderingsXml.Split(new [] {"<r"}, StringSplitOptions.None);
                foreach (string renderingXml in strings)
                {
                    if (renderingXml.Contains("s:ph=\"" + placeholderName + "\"") && renderingXml.Contains("uid=\"" + rendering.UniqueId + "\""))
                    {
                        renderingsXml = renderingsXml.Replace(renderingXml, renderingXml.Replace("s:ph=\"" + placeholderName + "\"", "s:ph=\"" + newPlaceholderName + "\""));
                    }
                }
            }
            item["__Renderings"] = renderingsXml;
            item.Editing.EndEdit();
        }

        foreach (Item child in item.GetChildren())
        {
            UpdatePlaceholderName(child, componentName, placeholderName, newPlaceholderName);
        }
    }
}