Copying full-screen rendering to a new template programmatically using renderDefinition.ItemId?

I have a custom sitecore button that changes the template of the current element, quite simple.

However, as part of this, I am also trying to transfer the rendering of the old layout to the new layout if it has a specific type of ItemId sublanguage. However, the returned ItemId always zero; the only value I return from RenderingDefinition is UniqueId .

What am I doing wrong?

I 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... } } } 
+6
source share
1 answer

In the end, I got Sitecore support that said that I should use:

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

instead:

 item[FieldIDs.LayoutField] 

to get the layoutField elements correctly. This leads to the correct analysis of the rendering values, and, as they say, the rest is history.

+6
source

All Articles