简体   繁体   中英

Temporarily change a Sitecore item's layout

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 temporarily, as I expected.

void ReplaceLayout(Item item)
{
    if (item == null)
        return;

    using (new SecurityDisabler())
    {
        // New item
        LayoutField newLayoutField = new LayoutField(item.Fields[Sitecore.FieldIDs.LayoutField]);
        LayoutDefinition newLayoutDefinition = LayoutDefinition.Parse(newLayoutField.Value);

        DeviceDefinition newDeviceDefinition = newLayoutDefinition.GetDevice(Sitecore.Context.Device.ID.ToString());

        // Current item
        LayoutField layoutField = new LayoutField(Sitecore.Context.Item.Fields[Sitecore.FieldIDs.LayoutField]);
        LayoutDefinition layoutDefinition = LayoutDefinition.Parse(layoutField.Value);

        DeviceDefinition deviceDefinition = layoutDefinition.GetDevice(Sitecore.Context.Device.ID.ToString());
        deviceDefinition.Layout = newDeviceDefinition.Layout;
        deviceDefinition.Renderings = newDeviceDefinition.Renderings;

        Sitecore.Context.Item.Editing.BeginEdit();
        layoutField.Value = layoutDefinition.ToXml();
        Sitecore.Context.Item.Editing.EndEdit();
    }
}

I don't want to make permenant changes to the item, I just want to replace the currently displayed items renderings on the fly if some conditions are met. Does anyone know how to alter an item's layout in this way?

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 (eg 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

Sitecore constructs the rendering controls and inserts them into the page very early in the ASP.NET Webforms lifecycle. It's not likely you can easily do this on the layout itself. However, if you can evaluate your conditions outside the page (eg by examining the item itself), you can probably do this in the insertRenderings pipeline.

Use ILSpy or another decompiler to check out Sitecore.Pipelines.InsertRenderings and Sitecore.Pipelines.InsertRenderings.Processors . As with other pipelines, the order of execution of these processors is defined in Web.config. You will want to add a new processor after AddRenderings which evaluates your conditions (likely examining args.ContextItem ) and then modifies args.Renderings as needed.

In your previous question you were just looking to remove sublayouts. That should be fairly easy here. Adding different sublayouts is more difficult since you would need to construct a RenderingReference . This may require that you actually create the XML definition needed for its constructor. Or, if you have another item that defines the desired new layout, consider just changing args.ContextItem earlier in the pipeline.

Rather than change sitecore presentation, can you not place the 'form' control and sidebar within one parent control container? You would then have an an easy id for a sidebar container with which to conditionally populate controls programatically from the form control.

Alternatively, could you add all the possible controls to the sidebar and 'activate' (or perhaps just make visible) the required control(s), maybe via session state variables? (I dont know if this falls foul of some lifecycle or timing limitiation)

或者,您可以简单地使用条件渲染规则,根据您可以想到的任何逻辑来显示所需的渲染/子布局。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM