mrtentje
mrtentje

Reputation: 1422

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

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

Upvotes: 3

Views: 3340

Answers (1)

mrtentje
mrtentje

Reputation: 1422

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]);

Upvotes: 4

Related Questions