Sitecore 8: Automatically fill a placeholder by a default rendering

拜拜、爱过 提交于 2019-12-10 15:01:16

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/33031246/sitecore-8-automatically-fill-a-placeholder-by-a-default-rendering

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!