In one of my previous article I discussed how we can retrieve data in a SharePoint List Using its Web Services. In this post I’m going to discuss how we can update a SharePoint list using Web Services. Please refer my previous article on "SharePoint List Web Service GetListItems" to learn how to add Web References to your project. Then you can use the following sample codes to Update SharePoint lists.
Updating Existing Items
In the following code I have update the "Tasks" list, there I have update the title of two items which has the ID 7 and 10.
public void updateListItemsWS() { WS_Lists.Lists myservice = new WS_Lists.Lists(); myservice.Credentials = System.Net.CredentialCache.DefaultCredentials; myservice.Url = "http://mermoss:5050/testsara/_vti_bin/Lists.asmx"; try { System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument(); System.Xml.XmlElement elBatch = xmlDoc.CreateElement("Batch"); elBatch.SetAttribute("OnError", "Continue"); elBatch.SetAttribute("ListVersion", "1"); string strBatch = "<Method ID='1' Cmd='Update'>" + "<Field Name='ID'>7</Field>" + "<Field Name='Title'>Sara1</Field></Method>" + "<Method ID='2' Cmd='Update'><Field Name='ID' >10</Field>" + "<Field Name='Title'>Sara2</Field></Method>"; elBatch.InnerXml = strBatch; myservice.UpdateListItems("Tasks", elBatch); } catch (Exception ex) { Response.Write(ex.Message); } }Delete Items
To delete item, use following phrase in the above code, this will delete the item which has ID of 10.
string strBatch = "<Method ID='1' Cmd='Delete'>" +
"<Field Name='ID'>10</Field></Method>";
"<Field Name='ID'>10</Field></Method>";
Add New Item
To add item, use following phrase in the above code,
string strBatch = "<Method ID='1' Cmd='New'>" +
"<Field Name='ID'>New</Field>"+
"<Field Name='Title'>TestTitle1</Field>"+
"</Method>";
"<Field Name='ID'>New</Field>"+
"<Field Name='Title'>TestTitle1</Field>"+
"</Method>";