Sitecore以编程方式添加布局。值不能为空。参数名称Path

时间:2011-11-27 17:50:06

标签: c# asp.net sitecore sitecore6

在我之前的问题中,我想出了如何在Sitecore中以编程方式添加布局,当布局和子布局与标准值相同时,这种方法很有效。但是当这个不同时,我得到的值不能为空。参数名称路径错误。

我以编程方式添加布局,因为要求在选中复选框时添加移动布局。并且在未选中复选框时删除布局。

我在互联网上搜索并在Sitecore中为{_Renderings字段添加了fix,但this无法解决问题。

添加Layout +子布局的代码:

        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();
            }
        }
    }

删除布局+子布局的代码:

        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();
            }
        }
    }

有没有人有任何建议/答案?我使用的是sitecore 6.4.1

提前致谢

1 个答案:

答案 0 :(得分:4)

一位同事(Sitecore认证的开发人员)向Sitecore发送了一张票......他们发送了以下答案:

尝试使用以下代码:

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

而不是:

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