title: Sitecore - set controls order in C#
body: <p>I want to change controls order in presentation details for one placeholder. I got renderings using <code>NewsletterHeader["__Renderings"]</code> and I had this:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;r xmlns:p="p" xmlns:s="s" p:p="1"&gt;
    &lt;d id="{FE5D7FDF-89C0-4D99-9AA3-B5FBD009C9F3}"&gt;
      &lt;r uid="{042E3CC4-8205-4A1D-85F4-7D667622711D}" s:ds="" s:par="" s:ph="masterplaceholder" /&gt;
      &lt;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" /&gt;            
      &lt;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" /&gt;            
      &lt;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" /&gt;         
      &lt;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" /&gt;           
      &lt;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" /&gt;            
      &lt;r uid="{F75C5CBE-60E2-4A0A-87F6-B6AB46AC2E52}" s:id="{71B654BA-5F47-4053-8936-7199C34E55E4}" s:ph="footerplaceholder" /&gt;
    &lt;/d&gt;
  &lt;/r&gt;
  </code></pre>
  
  <p>Then I tried to change its order in C# using this code:</p>
  
  <pre class="lang-csharp prettyprint-override"><code>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 &lt; order.Count; i++)
          {
              var section = order[i];
              if (i == 0)
              {
                  XElement sectionElement = xd.Descendants("r").FirstOrDefault(e =&gt; (e.Attribute(nsS + "ds") != null) &amp;&amp; e.Attribute(nsS + "ds").Value.Contains(section.Item1));
                  XElement footerElement = xd.Descendants("r").FirstOrDefault(e =&gt; (e.Attribute(nsS + "ph") != null) &amp;&amp; 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 =&gt; (e.Attribute(nsS + "ds") != null) &amp;&amp; e.Attribute(nsS + "ds").Value.Contains(section.Item1));
                  XElement prevSectionElement = xd.Descendants("r").FirstOrDefault(e =&gt; (e.Attribute(nsS + "ds") != null) &amp;&amp; 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();
      }
  }
  </code></pre>
  
  <p>As a result I received this xml:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;r xmlns:p="p" xmlns:s="s" p:p="1"&gt;
    &lt;d id="{FE5D7FDF-89C0-4D99-9AA3-B5FBD009C9F3}"&gt;
      &lt;r uid="{042E3CC4-8205-4A1D-85F4-7D667622711D}" s:ds="" s:par="" s:ph="masterplaceholder" /&gt;
      &lt;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" /&gt;
      &lt;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" /&gt;
      &lt;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" /&gt;
      &lt;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" /&gt;
      &lt;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" /&gt;
      &lt;r uid="{F75C5CBE-60E2-4A0A-87F6-B6AB46AC2E52}" s:id="{71B654BA-5F47-4053-8936-7199C34E55E4}" s:ph="footerplaceholder" /&gt;
    &lt;/d&gt;
  &lt;/r&gt;
  </code></pre>
  
  <p>And this is what I wanted to get. But when I enter presentation details in Sitecore, there is order like this:</p>
  
  <ul>
  <li>BANER</li>
  <li>JESZCZE-WIECEJ-HITOW</li>
  <li>HITY</li>
  <li>VOD</li>
  <li>Polecane</li>
  <li>footerplaceholder</li>
  </ul>
  
  <p>What am I doing wrong? Is there any other way to change this order?</p>