开发者

Associate the layout item to the item nodes programmatically in SiteCore

开发者 https://www.devze.com 2023-03-18 19:01 出处:网络
I am programmatically creating content tree item nodes using data in an xls. I am facing issues in associating layout to the item nodes. I am unable to identify any setter method for an item that I ca

I am programmatically creating content tree item nodes using data in an xls. I am facing issues in associating layout to the item nodes. I am unable to identify any setter method for an item that I can use. 开发者_如何转开发I thought of using the Item.Visualization properties but it did not help. Please can someone advice or help with a sample code of how to associate the layout item to the item nodes programmatically.


I understand why you are finding this hard, as it isn't very intuitive. I have made some sample code on how to do it and will probably write up a blog post about it later. Untill then here is the 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 make sense from that, otherwise it will be more explanatory, when I get time to write the blog post.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号