Sitecore programmatically add layout. Value cannot not be null. Parameter name Path

偶尔善良 提交于 2019-12-12 14:54:35

问题


In my previous question I figured out how to add a layout programmatically in Sitecore, this works fine when the layouts and sublayouts are the same as the Standard Values. But when this is different I get a Value cannot not be null. Parameter name Path error.

I am adding a layout programatically because the requirements are to add a mobile layout when a checkbox is checked. And remove to layout when the checkbox is not checked.

I've searched on the internet and added the fix from Sitecore for the __Renderings field, but this doesn't solve the problem.

The code to add a Layout + sublayout:

        protected void AddMobileLayout(Item item)
    {
        using (new SecurityDisabler())
        {
            LayoutDefinition layoutDefinition = Sitecore.Layouts.LayoutDefinition.Parse(item[Sitecore.FieldIDs.LayoutField]);
            DeviceDefinition mobileDevice = layoutDefinition.GetDevice(Resources.mobileDeviceID);
            TemplateItem itemTemplate = item.Template;

            if (itemTemplate != null)
            {
                if (itemTemplate.StandardValues != null)
                {
                    Item standardValues = itemTemplate.StandardValues;

                    foreach (DeviceItem deviceItem in Sitecore.Configuration.Factory.GetDatabase("master").Resources.Devices.GetAll())
                    {
                        if (deviceItem.ID.ToString() == Resources.mobileDeviceID)
                        {
                            mobileDevice.Layout = standardValues.Visualization.GetLayout(deviceItem).ID.ToString();
                            RenderingReference[] sublayouts = standardValues.Visualization.GetRenderings(deviceItem, true);

                            foreach (RenderingReference sublayout in sublayouts)
                            {
                                RenderingDefinition rendering = new RenderingDefinition();
                                rendering.Placeholder = sublayout.Placeholder;
                                rendering.ItemID = sublayout.RenderingItem.ID.ToString();

                                mobileDevice.AddRendering(rendering);
                            }
                        }
                    }
                }
            }
            item.Editing.BeginEdit();
            try
            {
                item.Fields[Sitecore.FieldIDs.LayoutField].Value = layoutDefinition.ToXml();
                item.Editing.EndEdit(false);
            }
            catch (System.Exception ex)
            {
                Sitecore.Diagnostics.Log.Error("Could not update item " + item.Paths.FullPath + ": " + ex.Message, this);
                item.Editing.CancelEdit();
            }
        }
    }

The code to remove the layout + sublayouts:

        protected void RemoveMobileLayout(Item item)
    {
        using (new SecurityDisabler())
        {
            LayoutDefinition layoutDefinition = Sitecore.Layouts.LayoutDefinition.Parse(item[Sitecore.FieldIDs.LayoutField]);
            DeviceDefinition mobileDevice = layoutDefinition.GetDevice(Resources.mobileDeviceID);

            if (mobileDevice.Layout != null) mobileDevice.Layout = null;
            if (mobileDevice.Renderings != null) mobileDevice.Renderings = new System.Collections.ArrayList();

            item.Editing.BeginEdit();
            try
            {
                item.Fields[Sitecore.FieldIDs.LayoutField].Value = layoutDefinition.ToXml();
                item.Editing.EndEdit(false);
            }
            catch (System.Exception ex)
            {
                Sitecore.Diagnostics.Log.Error("Could not update item " + item.Paths.FullPath + ": " + ex.Message, this);
                item.Editing.CancelEdit();
            }
        }
    }

Does anyone have any suggestions/answer(s)? I am using sitecore 6.4.1

Thanks in advance


回答1:


A colleague (who is a Sitecore certified developer) has send a ticket to Sitecore... They send the following answer:

Try to use the following code:

Sitecore.Layouts.LayoutDefinition.Parse(LayoutField.GetFieldValue(item.Fields[Sitecore.FieldIDs.LayoutField]))

instead of:

Sitecore.Layouts.LayoutDefinition.Parse(item[Sitecore.FieldIDs.LayoutField]);


来源:https://stackoverflow.com/questions/8287624/sitecore-programmatically-add-layout-value-cannot-not-be-null-parameter-name-p

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