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
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
Or you can simply use conditional rendering rules to display the renderings/sublayouts you want based on whatever logic you can dream up.
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 (e.g. 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)