Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sitecore 8: Automatically fill a placeholder by a default rendering

I was playing around with dynamic placeholders and was struck by a prefilling concept.Is there a way to select a default rendering for one of my placeholders which would avoid the "select rendering" dialog in experience editor ??

Scenario: I have a rendeing called "PageHead" which has three renderings. One of them is a placeholder "PageTeaserPh" which currently allows two renderings: one is "PageTeaser" and second "PageTeaserWithImage". I want the placeholder "PageTeaserPh" to always have the rendering selected as "PageTeaser" and therefore avoid the dialog "Select rendering" .

I did some homework and was wondering if this is something related to Standard values (we can have it at template level; not sure for renderings though) and also i have heard of command template concept (not in-depth).

Any and all help appreciated.

like image 374
Prathamesh dhanawade Avatar asked Nov 10 '22 03:11

Prathamesh dhanawade


1 Answers

You can have renderings assigned on standard values of templates, each new item would then have your PageTeaser rendering.

If you wanted to automate this process have a look at the <mvc.getXmlBasedLayoutDefinition> pipeline, we are injected common renderings by extending this pipeline.

Updated

I've found some code samples and blog posts that should help point you in the right direction for manipulating the layout details.

public void AddSublayoutToItem(string itemId, string sublayoutId)
{
    using (new Sitecore.SecurityModel.SecurityDisabler())
    {
        if (Sitecore.Data.ID.IsID(itemId) && Sitecore.Data.ID.IsID(sublayoutId))
        {
            //Get the master database and get the item on which you want to add sublayout
            Database masterDatabase = Database.GetDatabase("master");
            Item item = masterDatabase.GetItem(Sitecore.Data.ID.Parse(itemId));

            //  Or you can also get Sitecore Item from Context Database as per your requirement
            //  Item item = Sitecore.Context.Database.GetItem(Sitecore.Data.ID.Parse(itemId));

            if (item != null)
            {
                // Get the layout definitions and the device definition
                LayoutField layoutField = new LayoutField(item.Fields[Sitecore.FieldIDs.LayoutField]);
                LayoutDefinition layoutDefinition = LayoutDefinition.Parse(layoutField.Value);
                DeviceDefinition deviceDefinition = layoutDefinition.GetDevice(Sitecore.Context.Device.ID.ToString());

                //Create a RenderingDefinition and add the reference of sublayout or rendering
                RenderingDefinition renderingDefinition = new RenderingDefinition();
                renderingDefinition.ItemID = sublayoutId;
                //Set placeholder where the rendering should be displayed
                renderingDefinition.Placeholder = "content"; 
                // Set the datasource of sublayout, if any
                renderingDefinition.Datasource = "{24240FF2-B4AA-4EB2-B0A4-63E027934C38}";

                // you can also set datasource of sublayout using Sitecore Path
                // renderingDefinition.Datasource = "/sitecore/content/Home/Books";

                //Add the RenderingReference to the DeviceDefinition
                deviceDefinition.AddRendering(renderingDefinition);

                // Save the layout changes
                item.Editing.BeginEdit();
                layoutField.Value = layoutDefinition.ToXml(); ;
                item.Editing.EndEdit();
            }
        }
    }
}

Taken from here - http://www.bugdebugzone.com/2014/06/how-to-add-sublayout-to-sitecore-item.html

Also a couple of other blogs on the topic

like image 185
Wesley Lomax Avatar answered Jan 04 '23 01:01

Wesley Lomax