Compartilhar via


CAML Query to change list fields schema through Web service

The UpdateList method of lists web service takes few parameters, out of which newFields, UpdateFields, and DeleteFields are some of them.  These fields are actually XmlNode that contains CAML Query within it.  There are tools available to build CAML Queries.  But what about complex queries like changing properties of field at its schema level?  Here are some of the queries for Calculated  and Choice fields.

Calculated Fields:

ndUpdateFields.InnerXml = "<Method ID=\"1\"> <Field Type=\"Calculated\" Name=\"MyCalculatedColumn\" DisplayName=\" MyCalculatedColumn \" ResultType=\"Text\" Description=\"Description\"> <Formula>=Col_One*10</Formula><FieldRefs><FieldRef Name=\" Col_One \"/></FieldRefs></Field></Method>";

Note :-

We have to specify ResultType in the schema for calculated fields.  Otherwise in the UpdateList method call, we will get an exception of type 'Microsoft.SharePoint.SoapServer.SoapServerException' and the list will get corrupted.

Choice Fields Additional Settings:

Allow 'Fill-in' choices:

ndUpdateFields.InnerXml = "<Method ID=\"1\"><Field Type=\"Choice\" Name=\"MyChoiceColumn\" DisplayName=\"MyChoiceColumn\" Description=\"Description\" FillInChoice=\"TRUE\" ></Field></Method>";

Display choices using:

o Drop-Down Menu 

ndUpdateFields.InnerXml = "<Method ID=\"1\"><Field Type=\"Choice\" Name=\"MyChoiceColumn\" DisplayName=\"MyChoiceColumn\" Description=\"Description\" Format=\"Dropdown\" ></Field></Method>";

o Radio Buttons 

ndUpdateFields.InnerXml = "<Method ID=\"1\"><Field Type=\"Choice\" Name=\"MyChoiceColumn\" DisplayName=\"MyChoiceColumn\" Description=\"Description\" Format=\"RadioButtons\" ></Field></Method>";

o Checkboxes (allow multiple selections) 

ndUpdateFields.InnerXml = "<Method ID=\"1\"><Field Type=\"MultiChoice\" Name=\"MyChoiceColumn\" DisplayName=\"MyChoiceColumn\" Description=\"Description\" ></Field></Method>";

I have listed few CAML queries for some fields only.  There are lot of other properties for other fields as well.  The easiest approach to find out the actual schema of a field and find all available attributes is to use code to get the field schema.  Using SharePoint Object Model,

SPField customField = myList.Fields.GetField("MyCustomField");

string schemaXML = customField.SchemaXml;

Code to get field schema using Web Services

 listReference.Lists listService = new  listReference.Lists();
 listService.Credentials = System.Net.CredentialCache.DefaultCredentials;            
 XmlNode ndList = listService.GetList("MyList");
 XmlNodeList nodes = GetXPathQuery(ndList, "//sp:Field");
 foreach (XmlNode node in nodes)
 {
     try
     {
         string fieldXML = node.OuterXml; 
     }
     catch (Exception ex)
     {
       //Output exception message.
     }
 }
  
 private static XmlNodeList  GetXPathQuery(XmlNode XmlNodeToQuery, string XPathQuery)
 {
     XmlDocument Document = new XmlDocument();
     Document.LoadXml(XmlNodeToQuery.OuterXml);
     const string SharePointNamespacePrefix = "sp";
     const string SharePointNamespaceURI =
     "https://schemas.microsoft.com/sharepoint/soap/";
     const string ListItemsNamespacePrefix = "z";
     const string ListItemsNamespaceURI = "#RowsetSchema";
     const string PictureLibrariesNamespacePrefix = "y";
     const string PictureLibrariesNamespaceURI =
     "https://schemas.microsoft.com/sharepoint/soap/ois/";
     const string WebPartsNamespacePrefix = "w";
     const string WebPartsNamespaceURI =
     "https://schemas.microsoft.com/WebPart/v2";
     const string DirectoryNamespacePrefix = "d";
     const string DirectoryNamespaceURI =
     "https://schemas.microsoft.com/sharepoint/soap/directory/";
     const string DataRowSetNameSpacePrefix = "rs";
     const string DataRowSetNameSpaceURI = "urn:schemas-microsoft- com:rowset";
     XmlNamespaceManager NamespaceMngr = new XmlNamespaceManager(Document.NameTable);
     NamespaceMngr.AddNamespace(SharePointNamespacePrefix,
     SharePointNamespaceURI);
     NamespaceMngr.AddNamespace(ListItemsNamespacePrefix,
     ListItemsNamespaceURI);
     NamespaceMngr.AddNamespace(PictureLibrariesNamespacePrefix,
     PictureLibrariesNamespaceURI);
     NamespaceMngr.AddNamespace(WebPartsNamespacePrefix,
     WebPartsNamespaceURI);
     NamespaceMngr.AddNamespace(DirectoryNamespacePrefix,
     DirectoryNamespaceURI);
     NamespaceMngr.AddNamespace(DataRowSetNameSpacePrefix, DataRowSetNameSpaceURI);
     return Document.SelectNodes(XPathQuery, NamespaceMngr);
 }