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

发布于 2024-12-14 03:28:55 字数 3612 浏览 0 评论 0原文

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

我正在使用 Sitecore 6.4。

提前致谢!

我当前使用的代码(这是向项目添加硬编码布局,下一步是(当我解决此问题时)从 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();
        }
    }
}

I am facing the following problem: I want to add dynamically a mobile site for specific templates, I specified the mobile layout in the standard values of the specific item. This all works fine but when I changed a field of the Item the layout and renderings of the default site is gone! Does anyone has a solution/suggestion for this problem?

I am working with Sitecore 6.4.

Thanx in advance!

The code that I am currently using (this is to add hardcoded a layout to an item, the next step is (when I fixed this problem) to get the layout from the standard_values item)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清风不识月 2024-12-21 03:28:55

我已使用以下方法修复了此问题:

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

I've fixed this with the following to methods:

        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();
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文