Friday, February 18, 2011

How to reorder columns in Sharepoint Lists through Webservice


We have already seen how to reorder list columns through OM in my earlier post.

Let’s see how to achieve it through web service.

We are going to order the fields in the site content type that has been associated in the list through web service. The field ordering is done by the order in which <field> are presents within <Fields> tag. Hence we are going to reorder the fields by deleting the field tag and then adding them in the order we want. 

As we are going to make change in site content type we will use webs.asmx.
The following example will use UpdateContentType method of the webs.asmx to add and delete the fields in the content type.

Xml file

<?xml version="1.0" encoding="utf-8" ?>
<soapEnvelope>
  <soapBody>
    <UpdateContentType>
      <contentTypeId>0x010039AAF2FFAC8C2E468531F5B23B4595F2</contentTypeId>
      <contentTypeProperties>
        <ContentType Group="MyCT"/>
      </contentTypeProperties>
      <newFields>
        <Fields>
          <Method ID="1">
            <Field ID="{9fb22ff8-a1e6-46e5-9ee1-bcc1c95fab35}" Name="ColumeOrgFirst"/>
          </Method>
          <Method ID="2">
            <Field ID="{082f8d40-6f6b-438d-96dd-4a9958c08d8c}" Name="ColumeOrg2"/>
          </Method>
        </Fields>
      </newFields>

      <updateFields>
        <Fields>
        </Fields>
      </updateFields>

      <deleteFields>
        <Fields>
          <Method ID="1">
            <Field ID="{9fb22ff8-a1e6-46e5-9ee1-bcc1c95fab35}" Name="ColumeOrgFirst"/>
          </Method>
          <Method ID="2">
            <Field ID="{082f8d40-6f6b-438d-96dd-4a9958c08d8c}" Name="ColumeOrg2"/>
          </Method>
        </Fields>
      </deleteFields>

    </UpdateContentType>
  </soapBody>
</soapEnvelope>


Code

namespace CTOrder
{
    class Program
    {
        static void Main(string[] args)
        {
            webWS.Webs myWebs = new webWS.Webs();
            myWebs.Credentials = System.Net.CredentialCache.DefaultCredentials;

            myWebs.Url = "http://Site_URL/_vti_bin/webs.asmx";
            XmlReader myXmlReader = new XmlTextReader(@"ct.xml");
            XmlDocument myDoc = new XmlDocument();
            myDoc.Load(myXmlReader);
            XmlNode myProps = myDoc.SelectSingleNode("/soapEnvelope/soapBody/UpdateContentType/contentTypeProperties");
            XmlNode myDel = myDoc.SelectSingleNode("/soapEnvelope/soapBody/UpdateContentType/deleteFields");
            XmlNode myUpdate = myDoc.SelectSingleNode("/soapEnvelope/soapBody/UpdateContentType/updateFields");
            XmlNode myNew = myDoc.SelectSingleNode("/soapEnvelope/soapBody/UpdateContentType/newFields");
            XmlNode res = myWebs.UpdateContentType("0x010039AAF2FFAC8C2E468531F5B23B4595F2", myProps, myNew, myUpdate, myDel);

            myXmlReader.Close();

            Console.WriteLine(res.InnerXml);
            Console.ReadLine();
        }
    }
}


Happy Coding!!!

No comments:

Post a Comment