Sitecore-在C#中设置控制顺序

本文关键字:控制 顺序 设置 Sitecore- | 更新日期: 2023-09-27 18:00:55

我想更改一个占位符的演示详细信息中的控件顺序。我用NewsletterHeader["__Renderings"]得到了渲染图,我得到了这个:

<r xmlns:p="p" xmlns:s="s" p:p="1">
  <d id="{FE5D7FDF-89C0-4D99-9AA3-B5FBD009C9F3}">
    <r uid="{042E3CC4-8205-4A1D-85F4-7D667622711D}" s:ds="" s:par="" s:ph="masterplaceholder" />
    <r uid="{B19D0460-69C9-4FEC-A882-41FFA77B582F}" p:before="r[@uid='{D43901A0-1B71-4C65-BEED-DDCF28FCE5A5}']" s:ds="/sitecore/content/emagazyn/emagazyn-cplus/Polecane" s:id="{C3AC52FB-8F51-4B44-A3EE-55F4C2EAF83F}" s:par="" s:ph="moviesplaceholder" />            
    <r uid="{D43901A0-1B71-4C65-BEED-DDCF28FCE5A5}" p:before="r[@uid='{4D8EE7B4-E1D2-4CBF-AED7-5E969EFAA57A}']" s:ds="/sitecore/content/emagazyn/emagazyn-cplus/HITY" s:id="{7A80D822-8D6A-40C4-92D4-2FA22C596339}" s:par="" s:ph="moviesplaceholder" />            
    <r uid="{4D8EE7B4-E1D2-4CBF-AED7-5E969EFAA57A}" p:before="r[@uid='{B6036949-B723-42CE-9271-64DF299450DC}']" s:ds="/sitecore/content/emagazyn/emagazyn-cplus/VOD" s:id="{557800E7-953B-4830-8691-3FFB964712A8}" s:par="" s:ph="moviesplaceholder" />         
    <r uid="{B6036949-B723-42CE-9271-64DF299450DC}" p:before="r[@uid='{CC44C44C-B10C-417A-8E19-43B57AAD4C93}']" s:ds="/sitecore/content/emagazyn/emagazyn-cplus/BANER" s:id="{AE048833-A8AF-4668-938A-FEBA48281620}" s:par="" s:ph="moviesplaceholder" />           
    <r uid="{CC44C44C-B10C-417A-8E19-43B57AAD4C93}" p:before="r[@uid='{F75C5CBE-60E2-4A0A-87F6-B6AB46AC2E52}']" s:ds="/sitecore/content/emagazyn/emagazyn-cplus/JESZCZE-WIECEJ-HITOW" s:id="{D5FCDC02-C2A8-47A9-BCF5-99A3DF7E8A25}" s:par="" s:ph="moviesplaceholder" />            
    <r uid="{F75C5CBE-60E2-4A0A-87F6-B6AB46AC2E52}" s:id="{71B654BA-5F47-4053-8936-7199C34E55E4}" s:ph="footerplaceholder" />
  </d>
</r>

然后我尝试使用以下代码在C#中更改其顺序:

private void SetSectionsOrder()
{
    using (new SecurityDisabler())
    {
        //Get the value of the __renderings field
        string renderingXml = this.NewsletterHeader["__Renderings"];
        XNamespace nsS = "s";
        XNamespace nsP = "p";
        //Get order from model
        var order = this.Newsletter.GetSectionsOrder();
        //Generate XDcoument from xml
        XDocument xd = XDocument.Parse(renderingXml);
        for (int i = 0; i < order.Count; i++)
        {
            var section = order[i];
            if (i == 0)
            {
                XElement sectionElement = xd.Descendants("r").FirstOrDefault(e => (e.Attribute(nsS + "ds") != null) && e.Attribute(nsS + "ds").Value.Contains(section.Item1));
                XElement footerElement = xd.Descendants("r").FirstOrDefault(e => (e.Attribute(nsS + "ph") != null) && e.Attribute(nsS + "ph").Value == "footerplaceholder");
                if (footerElement != null)
                {
                    string footerUid = footerElement.Attribute("uid").Value;
                    if (sectionElement != null)
                        sectionElement.Attribute(nsP + "before").SetValue("r[@uid='" + footerUid + "']");
                }
            }
            else
            {
                var prevSection = order[i - 1];
                XElement sectionElement = xd.Descendants("r").FirstOrDefault(e => (e.Attribute(nsS + "ds") != null) && e.Attribute(nsS + "ds").Value.Contains(section.Item1));
                XElement prevSectionElement = xd.Descendants("r").FirstOrDefault(e => (e.Attribute(nsS + "ds") != null) && e.Attribute(nsS + "ds").Value.Contains(prevSection.Item1));
                if (prevSectionElement != null)
                {
                    string prevSectionUid = prevSectionElement.Attribute("uid").Value;
                    if (sectionElement != null)
                        sectionElement.Attribute(nsP + "before").SetValue("r[@uid='" + prevSectionUid + "']");
                }
            }
        }
        //Save new order   
        this.NewsletterHeader.Editing.BeginEdit();
        this.NewsletterHeader["__Renderings"] = xd.ToString();
        this.NewsletterHeader.Editing.EndEdit();
    }
}

结果我收到了这个xml:

<r xmlns:p="p" xmlns:s="s" p:p="1">
  <d id="{FE5D7FDF-89C0-4D99-9AA3-B5FBD009C9F3}">
    <r uid="{042E3CC4-8205-4A1D-85F4-7D667622711D}" s:ds="" s:par="" s:ph="masterplaceholder" />
    <r uid="{B19D0460-69C9-4FEC-A882-41FFA77B582F}" p:before="r[@uid='{F75C5CBE-60E2-4A0A-87F6-B6AB46AC2E52}']" s:ds="/sitecore/content/emagazyn/emagazyn-cplus/Polecane" s:id="{C3AC52FB-8F51-4B44-A3EE-55F4C2EAF83F}" s:par="" s:ph="moviesplaceholder" />
    <r uid="{D43901A0-1B71-4C65-BEED-DDCF28FCE5A5}" p:before="r[@uid='{4D8EE7B4-E1D2-4CBF-AED7-5E969EFAA57A}']" s:ds="/sitecore/content/emagazyn/emagazyn-cplus/HITY" s:id="{7A80D822-8D6A-40C4-92D4-2FA22C596339}" s:par="" s:ph="moviesplaceholder" />
    <r uid="{4D8EE7B4-E1D2-4CBF-AED7-5E969EFAA57A}" p:before="r[@uid='{B19D0460-69C9-4FEC-A882-41FFA77B582F}']" s:ds="/sitecore/content/NCPlus/Strony/emagazyn/emagazyn-cplus/VOD" s:id="{557800E7-953B-4830-8691-3FFB964712A8}" s:par="" s:ph="moviesplaceholder" />
    <r uid="{B6036949-B723-42CE-9271-64DF299450DC}" p:before="r[@uid='{CC44C44C-B10C-417A-8E19-43B57AAD4C93}']" s:ds="/sitecore/content/emagazyn/emagazyn-cplus/BANER" s:id="{AE048833-A8AF-4668-938A-FEBA48281620}" s:par="" s:ph="moviesplaceholder" />
    <r uid="{CC44C44C-B10C-417A-8E19-43B57AAD4C93}" p:before="r[@uid='{D43901A0-1B71-4C65-BEED-DDCF28FCE5A5}']" s:ds="/sitecore/content/emagazyn/emagazyn-cplus/JESZCZE-WIECEJ-HITOW" s:id="{D5FCDC02-C2A8-47A9-BCF5-99A3DF7E8A25}" s:par="" s:ph="moviesplaceholder" />
    <r uid="{F75C5CBE-60E2-4A0A-87F6-B6AB46AC2E52}" s:id="{71B654BA-5F47-4053-8936-7199C34E55E4}" s:ph="footerplaceholder" />
  </d>
</r>

这就是我想要得到的。但当我在Sitecore中输入演示详细信息时,会有这样的顺序:

  • BANER
  • 捷克
  • HITY
  • 视频点播
  • Polecane
  • 页脚占位符

我做错了什么?有其他方法可以改变这个订单吗?

Sitecore-在C#中设置控制顺序

顺序由uidp:before属性决定,因此结果是有意义的。

例如,BANER节点指定它应该出现在uid{CC44C44C-B10C-417A-8E19-43B57AAD4C93}的节点之前,即JESZCZE-WIECEJ-HITOW

JESZCZE-WIECEJ-HITOW节点指定它应该出现在uid{D43901A0-1B71-4C65-BEED-DDCF28FCE5A5}的节点之前,即HITY

唯一没有p:before属性的节点是footerplaceholder,正如预期的那样,它出现在最后。

因此,遵循这些规则,它们都按正确的顺序列出。

值得注意的是,Sitecore中有一个LayoutDefinition类。所以您不需要直接处理XML。

var ld = new LayoutDefinition();
ld.LoadXml(this.NewsletterHeader["__Renderings"]);

查看这些文章了解更多关于使用类的信息:

  • 使用SITECORE ASP.NET以编程方式更新布局细节厘米
  • 在Sitecore中以编程方式设置布局