Sitecore以编程方式添加移动网站

时间:2011-11-07 14:56:24

标签: c# layout sitecore sitecore6

我遇到以下问题:我想为特定模板动态添加移动网站,我在特定项目的标准值中指定了移动布局。这一切都很好但是当我更改了Item的一个字段时,默认网站的布局和渲染都消失了!有没有人对此问题有解决方案/建议?

我正在使用Sitecore 6.4。

提前Thanx!

我目前使用的代码(这是为项目添加硬编码布局,下一步是(当我修复此问题时)从standard_values项目获取布局)

    public class CheckMobileLayout
    {
    public void Process([NotNull] SaveArgs args)
    {
        try
        {
            foreach (Sitecore.Pipelines.Save.SaveArgs.SaveItem saveItem in args.Items)
            {
                Item orgItem = Context.ContentDatabase.Items[saveItem.ID, saveItem.Language, saveItem.Version];
                if(orgItem.Name != "Content Editor")
                {
                    TemplateItem testTemplate = orgItem.Template;

                    foreach (Field orgField in orgItem.Fields)
                    {
                        if (orgField != null)
                        {
                            if (orgField.GetTemplateField().Type == "Mobile Checkbox")
                            {
                                foreach (Sitecore.Pipelines.Save.SaveArgs.SaveField saveField in saveItem.Fields)
                                {
                                    if (saveField.ID == orgField.ID)
                                    {
                                        if (saveField.Value != orgField.Value)
                                        {
                                            if (saveField.Value == "1") AddMobileLayout(orgItem);
                                            else RemoveMobileLayout(orgItem);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        catch (NullReferenceException)
        {
        }
    }

    private void RemoveMobileLayout(Item orgItem)
    {
        using (new SecurityDisabler())
        {
            Database masterDatabase = Database.GetDatabase("master");
            orgItem = masterDatabase.GetItem(orgItem.Paths.Path);
            string renderingXml = orgItem[Strings.Renderings];

            LayoutDefinition layoutDefinition = new LayoutDefinition();
            layoutDefinition.LoadXml(renderingXml);

            string mobileDeviceId = Strings.mobileDeviceID;
            DeviceDefinition deviceDefinition = layoutDefinition.GetDevice(mobileDeviceId);

            deviceDefinition.Layout = String.Empty;

            string outputXml = layoutDefinition.ToXml();
            Log.Info(outputXml, this);
            orgItem.Editing.BeginEdit();
            orgItem[Strings.Renderings] = outputXml;
            orgItem.Editing.EndEdit();
        }
    }

    private void AddMobileLayout(Item orgItem)
    {
        using (new SecurityDisabler())
        {                
            Database masterDatabase = Database.GetDatabase("master");
            Item testItem = masterDatabase.GetItem(orgItem.Paths.Path);
            string renderingXml = testItem[Strings.Renderings];


            LayoutDefinition layoutDefinition = new LayoutDefinition();
            layoutDefinition.LoadXml(renderingXml);

            string mobileDeviceId = Strings.mobileDeviceID;
            DeviceDefinition deviceDefinition = layoutDefinition.GetDevice(mobileDeviceId);

            deviceDefinition.Layout = Strings.mobileLayoutID;

            string outputXml = layoutDefinition.ToXml();

            testItem.Editing.BeginEdit();
            testItem[Strings.Renderings] = layoutDefinition.ToXml();
            testItem.Editing.EndEdit();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

我用以下方法解决了这个方法:

        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 = null;

            item.Editing.BeginEdit();
            item[Sitecore.FieldIDs.LayoutField] = layoutDefinition.ToXml();
            item.Editing.EndEdit();
        }
    }

    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) mobileDevice.AddRendering(new RenderingDefinition() { ItemID = sublayout.RenderingItem.ID.ToString(), Placeholder = sublayout.RenderingItem.Placeholder });
                        }
                    }
                }
            }
            item.Editing.BeginEdit();
            item[Sitecore.FieldIDs.LayoutField] = layoutDefinition.ToXml();
            item.Editing.EndEdit();
        }
    }