6

I have a custom sitecore button which changes the template of the current item, simple enough.

However as part of this I'm trying to also migrate the renderings of the old layout to a new layout if it's of a certain sublayout type by ItemId. However the ItemId that is returned is always null, the only value I get back from the RenderingDefinition is the UniqueId.

What am I doing wrong?

I have used this blog post as a guide.

The Code

public class ConvertToNewTemplateCommand : Command
{
protected void Run(ClientPipelineArgs args)
{
    if (!SheerResponse.CheckModified())
        return;

    Item item = Context.ContentDatabase.Items[args.Parameters["id"]];
    if (args.IsPostBack)
    {
        if (args.Result == "yes")
        {
            //Get current layout details
            var originalLayoutXml = item[FieldIDs.LayoutField];

            //Get new template
            TemplateItem hubTemplate = Context.ContentDatabase.GetTemplate("some guid...");
            //Change template  
            item.ChangeTemplate(hubTemplate);
            //Reset laytout
            ResetLayout(item);
            //Get reset layout
            var newLayoutXml = item[FieldIDs.LayoutField];

            //Add all the module containers to the new layout in the central column
            MoveModuleContainers(item, originalLayoutXml, newLayoutXml);
        }
    }
}

private void MoveModuleContainers(Item item, string oldXml, string newXml)
{
    var oldLayout = LayoutDefinition.Parse(oldXml);
    var newLayout = LayoutDefinition.Parse(newXml);

    bool updated = false;

    var oldRenderings = (oldLayout.Devices[0] as DeviceDefinition).Renderings;
    var newRenderings = (newLayout.Devices[0] as DeviceDefinition).Renderings;

    foreach (RenderingDefinition rendering in oldRenderings)
    {
        // Here is where the rendering.ItemID is always null
        if (rendering != null && !String.IsNullOrEmpty(rendering.ItemID) && new Guid(rendering.ItemID) == new Guid("matching guid..."))
        {
            rendering.Placeholder = "middlecolumn";
            newRenderings.Add(rendering);
            updated = true;
        }
    }

    if (updated)
    {
                   // Save item...
            }
}
}
Yan Sklyarenko
  • 31,557
  • 24
  • 104
  • 139
danmac
  • 218
  • 2
  • 10

1 Answers1

7

I got onto Sitecore support in the end which informed me that I should use:

Sitecore.Data.Fields.LayoutField.GetFieldValue(item.Fields[Sitecore.FieldIDs.LayoutField])

instead of:

item[FieldIDs.LayoutField]

to get the items layoutField correctly. This results in the rendering values being parsed correctly and as they say the rest is history.

Chris
  • 7,996
  • 11
  • 66
  • 98
danmac
  • 218
  • 2
  • 10
  • +1 Sitecore support rocks! Thanks for sharing their feedback. – Dan Solovay Aug 08 '14 at 15:15
  • 1
    Whilst this answer got me there in the end (and rescued me from hours of trouble figuring out why the data sources weren't being returned), it's slightly incomplete. GetFieldValue is in fact a static method on the Sitecore.Data.Fields.LayoutField class. Took me a bit of time to figure that out, so hopefully this saves the next person a bit of confusion :) – Chris Nov 14 '14 at 21:32