Wednesday, December 10, 2008

Using Sharepoint Webservice to add/update/delete list Item

Using Sharepoint webservice, i have insert/update/delete list item through lists.asmx.
Using this code,just pass necessary parameter to run this code.

For Insert Item Query:

string caml="<Method ID=\"1\" Cmd=\"New\">" + "<Field Name=\"ID\">New</Field>" + "<Field Name=\"Title\">" + TextBox1.Text.ToString() + "</Field>" + "</Method>";

For Update Item Query:

string caml="<Method ID=\"1\" Cmd=\"New\">" + "<Field Name=\"ID\">1</Field>" + "<Field Name=\"Title\">" + TextBox1.Text.ToString() + "</Field> + "</Method>";

For Delete Item Query :

string caml="<Method ID=\"1\" Cmd=\"New\">" + "<Field Name=\"ID\">1</Field></Method>";

TP.WebService.ListOperation listItemAdd = new TP.WebService.ListOperation();

listItemAdd.InsertUpdateDeleteListItem("komal", "admin", komal, "http://komal:1222", caml, "test1");


public bool InsertUpdateDeleteListItem(string UserName, string Password, string Domain, string SiteURL,string CAML,string ListName)
{
try
{

ListData.Lists list = new ListData.Lists();
list.Url = SiteURL +"/_vti_bin/lists.asmx";
list.Credentials = new System.Net.NetworkCredential(UserName,Password,Domain);
System.Xml.XmlDocument xmlDoc;
System.Xml.XmlElement elBatch;
System.Xml.XmlNode ndReturn;
xmlDoc = new System.Xml.XmlDocument();
elBatch = xmlDoc.CreateElement("Batch");
elBatch = xmlDoc.CreateElement("Batch");
elBatch.SetAttribute("OnError", "Continue");
string strBatch = CAML;
elBatch.InnerXml = strBatch;
ndReturn = list.UpdateListItems(ListName, elBatch);
return true;

}

catch (Exception)
{
return false;
}
return true;
}