Monday 20 April 2015

Add , Update , Delete a list item using SharePoint web services

Update Items in the Task


Public void updateListItemsWS()
{
    WS_Lists.Lists myservice = new WS_Lists.Lists();
    myservice.Credentials = System.Net.CredentialCache.DefaultCredentials;
    myservice.Url = "http://site server/_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>";

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

No comments:

Post a Comment