Associate a layout item with item nodes programmatically in SiteCore

I programmatically create content tree element nodes using data in xls. I am facing problems in binding layout to element nodes. I cannot identify any setter method for an element that I can use. I was thinking about using Item.Visualization properties, but that didn't help. Please, someone can advise or help with a code sample on how to programmatically associate a layout element with element nodes.

+4
source share
1 answer

I understand why you find it difficult, because it is not very intuitive. I have made some code examples on how to do this, and maybe will write a blog post about this later. Until then, here is a sample code:

using(new SecurityDisabler()) { Database masterDatabase = Database.GetDatabase("master"); ID sampleLayoutId = new ID("{14030E9F-CE92-49C6-AD87-7D49B50E42EA}"); ID defaultDeviceId = new ID("{FE5D7FDF-89C0-4D99-9AA3-B5FBD009C9F3}"); ID sampleItemId = new ID("{2E4C98CF-DD72-4B55-9DF6-2F6691A6690B}"); ID sampleRenderingId = new ID("{493B3A83-0FA7-4484-8FC9-4680991CF743}"); Item sampleItem = masterDatabase.GetItem(sampleItemId); Item layoutItem = masterDatabase.GetItem(sampleLayoutId); LayoutDefinition layoutDefinition = new LayoutDefinition(); layoutDefinition.LoadXml(sampleItem["__Renderings"]); DeviceDefinition deviceDefinition = layoutDefinition.GetDevice(defaultDeviceId.ToString()); deviceDefinition.ID = defaultDeviceId.ToString(); deviceDefinition.Layout = sampleLayoutId.ToString(); RenderingDefinition renderingDefinition = new RenderingDefinition(); renderingDefinition.ItemID = sampleRenderingId.ToString(); deviceDefinition.AddRendering(renderingDefinition); sampleItem.Editing.BeginEdit(); sampleItem["__Renderings"] = layoutDefinition.ToXml(); sampleItem.Editing.EndEdit(); } 

I hope you can understand this, otherwise it will be more explanatory when I manage to write a blog post.

+5
source

All Articles