Monday, January 5, 2009

How to add,update,delete site columns by using SharePoint's Webs WebService

I have found examples on how to do this, unfortunately none of them worked.  The missing attribute of ID from the newFields node was the issue with the examples.  The following should give you the idea.

            //webs webservice
wsWebs.Webs websService = new SiteColumnTester.wsWebs.Webs();

//url for the webservice
websService.Url = "http://localhost/sites/Tony/_vti_bin/Webs.asmx";

websService.Credentials = System.Net.CredentialCache.DefaultCredentials;

//xmldocument object
XmlDocument xDoc = new XmlDocument();

//Fields to be added
XmlNode newFields = xDoc.CreateNode(XmlNodeType.Element, "Fields", "");

//Fields to be updated
XmlNode updateFields = xDoc.CreateNode(XmlNodeType.Element, "Fields", "");

//Fields to be deleted
XmlNode deleteFields = xDoc.CreateNode(XmlNodeType.Element, "Fields", "");

//When adding a new field, make sure to give it an ID or it will not create the field
newFields.InnerXml = "<Method ID='1'><Field ID='{1C8452A0-F3BD-4a70-85D7-B35BF5EC8CE8}' Name='fieldName' DisplayName='fieldDisplayName' Hidden='FALSE' Type='Text'/></Method>";
//Update a field
updateFields.InnerXml = "<Method ID='2'><Field Name='fieldName' DisplayName='fieldDisplayName' Type='Text' Group='Tony'/></Method>";
//Delete a field
deleteFields.InnerXml = "<Method ID='3'><Field Name='fieldName'/></Method>";

XmlNode UpdateColumnsResponse = websService.UpdateColumns(newFields, updateFields, deleteFields);


Happy Coding!