0

How to add a List Item Using SharePoint Web service

SharePoint WebServices are one of the cool features provided which enables us usage of SharePoint capability in form of Web Methods and can be accessed from any other server / Application. We need to add a service reference to the project . The URL which should be used must be  http://<ServerName>/<SubsiteorSite(ifRequired)>/_vti_bin/Lists.asmx (for accessing SharePoint List).

Refere here for more

Now once the reference is added , Name it as per your case. i am using the reference name as “SRefiCentralList”.

Below code will add an item to the list. Change the code based on your requirement. You need to change the List Name and Field Names.

SRefiCentralList.ListsSoapClient wsClient = new SRefiCentralList.ListsSoapClient();
wsClient.ClientCredentials.Windows.AllowNtlm =true;
//Allow NTLM authenticattion

//Use Credential for a domain acoount, recommened a service account. This is required only for allowing user to add item impersonated.
wsClient.ClientCredentials.Windows.ClientCredential =
new NetworkCredential(<UserName>, <Password>, <Domain>);
wsClient.ClientCredentials.Windows.AllowedImpersonationLevel =System.Security.Principal.TokenImpersonationLevel.Impersonation;
//All Impersonation.
//Get List object in Xelement
XElement doc = wsClient.GetList(@"<List Name>");
//get list ID of List
listID = (from x in doc.Attributes() where x.Name.LocalName.Equals("ID" )
select x.Value).FirstOrDefault();
//New fields can be added as above while adding an item
XElement newItem = newXElement("Batch", newXAttribute("OnError", "Continue"),
newXAttribute("ListVersion", "1"), new XElement("Method",newXAttribute("ID", "1"),
newXAttribute("Cmd", "New"),newXElement("Field",newXAttribute("Name", "Title"),<Title Value>),
newXElement("Field",newXAttribute("Name", "Start_x0020_Time"),<start Time Value>),
newXElement("Field",newXAttribute("Name", "End_x0020_Time"),ChatUploadEndTime)));
XElement resCreateNew = wsClient.UpdateListItems(listID, newItem);
//Add an item to list.
//Add an attchment to the added new list item
string strListItemId = (from t in resCreateNew.Descendants().Attributes("ows_ID")
select t.Value).FirstOrDefault();
//xmlstringData is a string data which is converted to string.which should be attached.
// Will update the List item with attachment.
string xmlString = wsClient.AddAttachment("<List Name>", strListItemId, "<File Name>, xmlstringData);

This is really helpful to update any list item away from the SharePoint Server.If your code is not excecuted on SharePoint server, only way out is using web services.All the best