Monday, December 28, 2009

ADD|Update|Delete Item SharePoint Web Services

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";
            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)
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>";

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>"+


  1. Thanx for these examples. It solves at least one of my problems. Is it possible to change the description of a listitem the same way? I can't seem to find this piece of info in the XML node


  2. Hey Marc,
    Please tell me what you mean by "description" ?

  3. Simple and Informative Thanks for sharing

  4. Hi,
    A "Save" button on my infopath form, when clicked
    1)submits the form
    2) updates certain set of columns present in the form library itself, with some values.
    This update operation is done, using "UpdateListItems" Method of the lists.asmx web service.
    Now, the problem is, when a form is newly created, this update works fine. But, if the same form is reopened, edited and saved, updates are not happening. Pls help.

  5. @ Anonymous,
    It worked fine for me, did you check after removing the cache in your browser ?