Tuesday, June 28, 2011

Delete List Item using web service

We might need this when you want to remove any list item from the list but from remote location, from other client.

Well, here is a simple way to do it. I am just taking an example of client server application button click. Code remains same where ever you want to use it.

Take the web service reference into your project from the SharePoint site.



protected void Button1_Click(object sender, EventArgs e)
{
Lists.Lists ListReference = new Lists.Lists();

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

ListReference.Url = "site_url/_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='Delete'>" +
"<Field Name='ID'>" + "4" + "</Field></Method>";

elBatch.InnerXml = strBatch;
ListReference.UpdateListItems("List Name", elBatch);


}
catch (Exception ex)
{
Response.Write(ex.Message);

}
}


All we need to do is include the ID in the query. In our case, we are deleting item with the ID 4. ID is the item ID that you want to delete from the list.

Read Remove all survey response from web service for some interesting stuff.

No comments:




Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com