How to: Update List Items
This programming task shows how to use the UpdateListItems method of the Lists Web service to update items in a list through a Microsoft Windows application.
Use a System.Xml.XmlElement object to create a Batch element in Collaborative Application Markup Language (CAML) that can contain multiple Method elements, and use the UpdateListItems method to post the methods and update items. The Cmd attribute of each Method element determines the operation that is performed on an item by specifying one of the following values:
-
Delete -- Delete an item.
-
New -- Create an item.
-
Update -- Modify an item.
Procedures
Before you begin, create a Windows application as described in Getting Started with Programmatically Customizing a SharePoint Web Site in Visual Studio. For information about setting a Web reference to a Windows SharePoint Services Web service, see Web Service Guidelines.
To add code to update list items
-
Open Form1 in Design view, open the Toolbox, and then drag a Button control onto the form.
-
Double-click the Button1 control to display the code editor, and then add the following lines of code to the Click event handler:
'Declare and initialize a variable for the Lists Web service. Dim listService As New sitesWebServiceLists.Lists() 'Authenticate the current user by passing their default 'credentials to the Web service from the system credential cache. listService.Credentials = System.Net.CredentialCache.DefaultCredentials 'Set the Url property of the service for the path to a subsite. listService.Url = "http://MyServer/sites/MySiteCollection/_vti_bin/Lists.asmx" 'Get Name attribute values (GUIDs) for list and view. Dim ndListView As System.Xml.XmlNode = listService.GetListAndView("MyList", "") Dim strListID As String = ndListView.ChildNodes(0).Attributes("Name").Value Dim strViewID As String = 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. Dim doc As New System.Xml.XmlDocument() Dim batchElement As System.Xml.XmlElement = 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 columns. batchElement.InnerXml = "<Method ID='1' Cmd='Update'>" + "<Field Name='ID'>6</Field>" + "<Field Name='Title'>Modified sixth item</Field></Method>" + "<Method ID='2' Cmd='Update'><Field Name='ID'>7</Field>" + "<Field Name='Title'>Modified seventh item</Field></Method>" + "<Method ID='3' Cmd='Delete'><Field Name='ID'>5</Field>" + "</Method><Method ID='4' Cmd='New'>" + "<Field Name='Title'>Added item</Field></Method>" 'Update list items. This example uses the list GUID, which is recommended, but the list display name will also work. listService.UpdateListItems(strListID, batchElement)
/*Declare and initialize a variable for the Lists Web service.*/ sitesWebServiceLists.Lists listService = new sitesWebServiceLists.Lists(); /*Authenticate the current user by passing their default credentials to the Web service from the system credential cache.*/ listService.Credentials = System.Net.CredentialCache.DefaultCredentials; /*Set the Url property of the service for the path to a subsite.*/ listService.Url = "http://MyServer/sites/MySiteCollection/_vti_bin/Lists.asmx"; /*Get Name attribute values (GUIDs) for list and view. */ System.Xml.XmlNode ndListView = listService.GetListAndView("MyList", ""); 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 = "<Method ID='1' Cmd='Update'>" + "<Field Name='ID'>6</Field>" + "<Field Name='Title'>Modified sixth item</Field></Method>" + "<Method ID='2' Cmd='Update'><Field Name='ID'>7</Field>" + "<Field Name='Title'>Modified seventh item</Field></Method>" + "<Method ID='3' Cmd='Delete'><Field Name='ID'>5</Field>" + "</Method><Method ID='4' Cmd='New'>" + "<Field Name='Title'>Added item</Field></Method>"; /*Update list items. This example uses the list GUID, which is recommended, but the list display name will also work.*/ try { listService.UpdateListItems(strListID, batchElement); } catch (SoapServerException ex) { MessageBox.Show(ex.Message); }
Note:
Posting the UpdateListItems method silently fails if a specified item does not exist. The ID for a deleted item is maintained after a delete operation. Consequently, the example deletes the fifth item in the list, but 5 is not reassigned as the ID for another item.
-
On the Debug menu click Start Debugging, or press F5, to test the form.
- 1/20/2012
- SilvanoJulio
http://www.binaryjam.com/2012/01/04/error-when-talking-to-sharepoint-web-services-in-code/
Perhaps is MS commented the code especially the line before setting the URL to say don't comment this out, your reference will not automatically work,
you have to set it as the soap:address is generic and thats where it will be posted.
- 1/4/2012
- Binary Jam
I want to be able to do something along the lines of:
<Batch OnError='Continue'>
<Method ID='1' Cmd='Update'>
<Field Name='DocumentType'>1;#Blend process</Field>
<Where><Eq><FieldRef Name='LinkFilename'/><Value Type='Text'>FileName.docx</Value></Eq></Where>
</Method>
</Batch>
I currently don't know the ID of the record in the document library I want to update. I only know the file name, since I have just uploaded the file using CopyIntoItems() and that function only returns the file name, which is no help since I specify the file name anyway.
I need a way to update records based on some other parameter such as the file name.
Note: I would have just specified my field value as part of CopyIntoItems() using the FieldInformation array, except there seems to be some bug in CopyIntoItems() , where by lookup values are just ignored. (such as DocumentType above)
- 3/26/2010
- Aceofspades25
- 10/3/2010
- Thomas Lee
If you need to update multi value fields (lookup, user or choice) using the UpdateListItems method you can use the following format:
<Batch OnError='Continue'>
<Method ID='1' Cmd='Update'>
<Field Name='ID'>2</Field>
<Field Name='Location'>1;#;#2</Field>
<Field Name='Owners'>1;#;#7</Field>
<Field Name='Choices'>Value1;#Value2</Field>
</Method>
</Batch>
For the full coverage of the topic see this post:
http://pholpar.wordpress.com/2010/01/26/updating-multi-value-fields-using-web-service-call-and-batch-update/
- 1/26/2010
- Peter Holpar
- 12/2/2009
- mike.gaal
The correct syntax is probably:
<Method ID='1' Cmd='Update'>
<Field Name='ID'>1</Field>
<Field Name='Last_x0020_Distributed'>2009-09-15 14:44:44</Field>
</Method>
In order to validate this please view the HTML source of your SharePoint item page (DispForm.aspx) and check the FieldInternalName which will be commented below that field. That is the name that should be in the Name attribute when you are attempting to run updates. For example:
<!-- FieldName="Last Distributed"
FieldInternalName="Last_x0020_Distributed"
FieldType="SPFieldDateTime"
-->
Hope that helps,
Mark
- 9/29/2009
- Mark Cicoria
- 9/29/2009
- Mark Cicoria
I get the above error when running the code. The InnerXml property of the batchElement is as follows: -
<Method ID='1' Cmd='Update'><Field Name='ows_ID'>1</Field><Field Name='ows_Last_x0020_Distributed'>2009-09-15 14:44:44</Field></Method>
If I change this to: -
<Method ID='1' Cmd='Update'><Field Name='ID'>1</Field><Field Name='Last Distributed'>2009-09-15 14:44:44</Field></Method>
I get the following error: -
"0x81020014One or more field types are not installed properly. Go to the list settings page to delete these fields."
Which one should work, and why doesn't it?
Stuart.
- 9/15/2009
- stuartwhiteford