Temporarily change a Sitecore item's layout

后端 未结 4 1062
小蘑菇
小蘑菇 2021-01-25 10:58

Using this code I managed to change the renderings on the current item. However this changed it permenantly in Sitecore (the changes were could be seen in the CMS) and not tempo

4条回答
  •  迷失自我
    2021-01-25 11:09

    You explained in your comments that you want to display certain sublayouts in the sidebar depending on certain form parts/steps. You can do that by adding a PlaceHolder that will fit the sublayouts (e.g. in your sidebar) and use this code to dynamically render sublayouts to it.

    First you need an item (i call it a snippet item) that has the sublayout configured on its presentation settings. Then you can use code to render that item inside the placeholder (phSideBarPlaceHolder).

    // Load snippet item
    Item snippet = Sitecore.Context.Database.GetItem("{id-or-path-of-snippet-item}");
    
    // Get the first rendering from item's presentation definition
    RenderingReference rendering = snippet.Visualization.GetRenderings(Sitecore.Context.Device, false).FirstOrDefault();
    
    // We assume that its a Sublayout, but you can also check for xslt and create an XslFile() object
    Sublayout sublayout = new Sublayout();
    sublayout.DataSource = snippet.Paths.FullPath; // creates a reference to the snippet item, so you can pull data from that later on
    sublayout.Path = rendering.RenderingItem.InnerItem["Path"];
    sublayout.Cacheable = rendering.RenderingItem.Caching.Cacheable;
    
    // Copy cache settings
    if (rendering.RenderingItem.Caching.Cacheable)
    {
        sublayout.VaryByData = rendering.RenderingItem.Caching.VaryByData;
        sublayout.VaryByDevice = rendering.RenderingItem.Caching.VaryByDevice;
        sublayout.VaryByLogin = rendering.RenderingItem.Caching.VaryByLogin;
        sublayout.VaryByParm = rendering.RenderingItem.Caching.VaryByParm;
        sublayout.VaryByQueryString = rendering.RenderingItem.Caching.VaryByQueryString;
        sublayout.VaryByUser = rendering.RenderingItem.Caching.VaryByUser;
    }
    
    // Now render the sublayout to the placeholder
    phSideBarPlaceHolder.Controls.Add(sublayout);
    

    If you need more info about how to read data the DataSource property inside the sublayout code, Mark Ursino has written an article about that: http://firebreaksice.com/using-the-datasource-field-with-sitecore-sublayouts

提交回复
热议问题