How to: Update List Items using sharepoint web service
Introduction:
In this article I am going to explain you that How to update list item using sharepoint web service.
To do this we will be using Lists.asmx web service and UpdateListItems web method.
UpdateListItems Adds, deletes, or updates the specified items in a list on the current site.
Syntex Code:-
public XmlNode UpdateListItems (string listName,XmlNode updates)
Here “updates” variable is a xml specific format batch which explain about action.
The Sample Code:-
Before I go to sample code I will give below basic example of xml batch which has information to update sharepoint list:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <Method ID="1" Cmd="Update"> <Field Name="ID">4<Field> <Field Name="Field_Name">Value</Field> </Method> <Method ID="2" Cmd="New"> <Field Name="ID" >6</Field> <Field Name="Field_Name">Value</Field> </Method> <Method ID="3" Cmd="Update"> <Field Name="ID" >8</Field> <Field Name="Field_LookUp_Name">ID</Field> </Method> <Method ID="4" Cmd="Delete"> <Field Name="ID" >16</Field> <Field Name="Field_Name">Value</Field> </Method> |
Here for every item update you will have method id (its your own incremental id which does not have any relation with share point list id). Inside method node; 1st element would be field name “ID” which is share point item ID and 2nd field onward its item column name & value which needs to update.
Note: For lookup column you need to put lookup column value id.
Once the xml is ready you need to call below function which will do update in sharepoint list:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | /// <summary> /// Update metadata of document in document library /// </summary> /// <param name="strListName">list name</param> /// <param name="strCAML">metadata information in query</param> public void UpdateMetaData(string strListName, string strCAML) { Lists.Lists listReference = new AvinashNamespace.Lists.Lists(); try { listReference.Credentials = credentials; // get defualt NetworkCredential credentials // get web service url listReference.Url = settings.SiteUri.ToString().TrimEnd('/') + "/_vti_bin/lists.asmx"; /*Get Name attribute values (GUIDs) for list and view. */ System.Xml.XmlNode ndListView = listReference.GetListAndView(strListName, ""); string strListID = ndListView.ChildNodes[0].Attributes["Name"].Value; string strViewID = ndListView.ChildNodes[1].Attributes["Name"].Value; /*Create an XmlDocument object and construct a Batch element and its attributes. Note that an empty ViewName parameter causes the method to use the default view. */ System.Xml.XmlDocument doc = new System.Xml.XmlDocument(); System.Xml.XmlElement batchElement = doc.CreateElement("Batch"); batchElement.SetAttribute("OnError", "Continue"); batchElement.SetAttribute("ListVersion", "1"); batchElement.SetAttribute("ViewName", strViewID); /*Specify methods for the batch post using CAML. To update or delete, specify the ID of the item, and to update or add, specify the value to place in the specified column.*/ batchElement.InnerXml = strCAML; /*Update list items. This example uses the list GUID, which is recommended, but the list display name will also work.*/ listReference.UpdateListItems(strListID, batchElement); } catch (Exception ex) { // handle exception } finally { if (listReference != null) listReference.Dispose(); } } |
References:-
Refer msdn link for various xml example
http://msdn.microsoft.com/en-us/library/lists.lists.updatelistitems(v=office.12).aspx
Thanks!
Avinash
March 20, 2012
В·
Infoyen В·
One Comment
Tags: How to: Update List Items, Lists web service, Lists.UpdateListItems Method (Lists), Update taxonomy field values in a list item through SharePoint web services, Web Service В· Posted in: MOSS, SharePoint, Web Service
One Response
Hello,
Thank you for sample. I have a quick question, since i am by no means, a SharePoint expert, on how to find the strListName. In otherwords you are calling your function and passing a list name to it.
so if my document that i am attempting to update its meta data is located in:
https://spsite/folder1/folder2/Shared%20Documents/abc5.txt
what would the list name that i need to pass to your method be?
Leave a Reply