25 out of 97 rated this helpful - Rate this topic

Lists.UpdateListItems Method

Windows SharePoint Services 3
Adds, deletes, or updates the specified items in a list on the current site.

Web Service: ListsWeb Reference: http://<Site>/_vti_bin/Lists.asmx
[SoapDocumentMethodAttribute("http://schemas.microsoft.com/sharepoint/soap/UpdateListItems", RequestNamespace="http://schemas.microsoft.com/sharepoint/soap/", ResponseNamespace="http://schemas.microsoft.com/sharepoint/soap/", Use=SoapBindingUse.Literal, ParameterStyle=SoapParameterStyle.Wrapped)] 
public XmlNode UpdateListItems (
	string listName,
	XmlNode updates
)

Parameters

listName

A string that contains the name of the list. It is recommended that you use the list GUID surrounded by curly braces (i.e., "{GUID}"), but you can also use the list display name.

updates

A Batch element that contains one or more methods for adding, modifying, or deleting items and that can be assigned to a System.Xml.XmlNode object.

The following example shows how to modify column values for two specified items.

<Batch OnError="Continue" ListVersion="1" 
ViewName="270C0508-A54F-4387-8AD0-49686D685EB2">
   <Method ID="1" Cmd="Update">
      <Field Name="ID">4<Field>
      <Field Name="Field_Name">Value</Field>
   </Method>
   <Method ID="2" Cmd="Update">
      <Field Name="ID" >6</Field>
      <Field Name="Field_Name">Value</Field>
   </Method>
</Batch>

An empty ViewName attribute in the Batch element causes the default view to be used. The ID attribute in each Method element uniquely identifies the specific update so that errors and return values can be properly identified. Each method that is posted contains Field elements that specify the ID of the item and the new field value for the item. The field ID does not correspond to the index of the item in the collection of items for the list.

The following example shows the format for adding a new item that contains both a Date value and a DateTime value.

<Batch OnError="Continue" ListVersion="1" 
ViewName="270C0508-A54F-4387-8AD0-49686D685EB2">
   <Method ID="1" Cmd="New">
      <Field Name='ID'>New</Field>
      <Field Name="Title">Value</Field>
      <Field Name="Date_Column">2007-3-25</Field>
      <Field Name="Date_Time_Column">
         2006-1-11T09:15:30Z</Field>
   </Method>
</Batch>

The following example shows how to delete two items.

<Batch OnError="Continue" ListVersion="1" 
ViewName="270C0508-A54F-4387-8AD0-49686D685EB2">
   <Method ID="1" Cmd="Delete">
      <Field Name='ID'>2</Field>
   </Method>
   <Method ID="2" Cmd="Delete">
      <Field Name='ID'>8</Field>
   </Method>
</Batch>
NoteNote:

IDs for deleted items are maintained after delete operations. Consequently, the example deletes the second and eighth items in the list, but 2 and 8 are not reassigned as the IDs of other items.

For descriptions of the various formats used for field types that can be passed in this parameter see SPListItem.

The following examples show the methods that can be posted for operations related to document libraries:

  • Create a folder

    <Batch OnError="Continue" PreCalc="TRUE" 
    ListVersion="0" 
    ViewName="{EF2F5A21-0FD0-4654-84ED-112B4F5A48F8}">
       <Method ID="1" Cmd="New">
          <Field Name="ID">New</Field>
          <Field Name="FSObjType">1</Field>
          <Field Name="BaseName">Name</Field>
       </Method>
    </Batch>
    
  • Update a folder

    <Batch OnError="Continue" PreCalc="TRUE" 
    ListVersion="0" 
    ViewName="{EF2F5A21-0FD0-4654-84ED-112B4F5A48F8}">
       <Method ID="1" Cmd="Update">
          <Field Name="ID">3</Field>
          <Field Name="owshiddenversion">1</Field>
          <Field Name="FileRef">
             http://Server/[sites/][Site/]Shared 
             Documents/Folder</Field>
          <Field Name="FSObjType">1</Field>
          <Field Name="BaseName">Name</Field>
       </Method>
    </Batch>
    
  • Delete a folder

    <Batch OnError="Continue" PreCalc="TRUE" 
    ListVersion="0" 
    ViewName="{EF2F5A21-0FD0-4654-84ED-112B4F5A48F8}">
       <Method ID="1" Cmd="Delete">
          <Field Name="ID">4</Field>
          <Field Name="FileRef">
             http://Server/[sites/][Site/]Shared 
             Documents/Folder</Field>
       </Method>
    </Batch>
    
  • Update documents

    <Batch OnError="Continue" PreCalc="TRUE" 
    ListVersion="0" 
    ViewName="{EF2F5A21-0FD0-4654-84ED-112B4F5A48F8}">
       <Method ID="1" Cmd="Update">
          <Field Name="ID">2</Field>
          <Field Name="owshiddenversion">1</Field>
          <Field Name="FileRef">
             http://Server/[sites/][Site/]Shared 
             Documents/File</Field>
          <Field Name="BaseName">Name</Field>
       </Method>
    </Batch>
    
  • Delete documents

    <Batch OnError="Continue" PreCalc="TRUE" 
    ListVersion="0" 
    ViewName="{EF2F5A21-0FD0-4654-84ED-112B4F5A48F8}">
       <Method ID="1" Cmd="Delete">
          <Field Name="ID">3</Field>
          <Field Name="FileRef">
             http://Server/[sites/][Site/]Shared 
             Documents/File</Field>
       </Method>
    </Batch>
    

Return Value

An XMLDATA fragment in the following form that shows the status of each method block posted through the updates parameter and that can be assigned to a System.Xml.XmlNode object. For items successfully updated, a row fragment is returned with the updated row values.
<Results xmlns="http://schemas.microsoft.com/sharepoint/soap/">
   <Result ID="1,Update">
      <ErrorCode>0x00000000</ErrorCode>
      <z:row ows_ID="4" ows_Title="Title" 
         ows_Modified="2003-06-19 20:31:21" 
         ows_Created="2003-06-18 10:15:58" 
         ows_Author="3;#User1_Display_Name" 
         ows_Editor="7;#User2_Display_Name" ows_owshiddenversion="3" 
         ows_Attachments="-1" 
         ows__ModerationStatus="0" ows_LinkTitleNoMenu="Title" 
         ows_LinkTitle="Title" 
         ows_SelectTitle="4" ows_Order="400.000000000000" 
         ows_GUID="{4962F024-BBA5-4A0B-9EC1-641B731ABFED}" 
         ows_DateColumn="2003-09-04 00:00:00" 
         ows_NumberColumn="791.00000000000000" 
         xmlns:z="#RowsetSchema" />
   </Result>
   <Result ID="2,Update">
      <ErrorCode>0x00000000</ErrorCode>
      <z:row ows_ID="6" ows_Title="Title" 
         ows_Modified="2003-06-19 20:31:22" 
         ows_Created="2003-06-18 19:07:14" 
         ows_Author="2;#User1_Display_Name" 
         ows_Editor="6;#User2_Display_Name" ows_owshiddenversion="4" 
         ows_Attachments="0" ows__ModerationStatus="0" 
         ows_LinkTitleNoMenu="Title" 
         ows_LinkTitle="Title" ows_SelectTitle="6" 
         ows_Order="600.000000000000" 
         ows_GUID="{2E8D2505-98FD-4E3E-BFDA-0C3DEBE483F7}" 
         ows_DateColumn="2003-06-23 00:00:00" 
         ows_NumberColumn="9001.00000000000000" 
         xmlns:z="#RowsetSchema" />
   </Result>
   ...
</Results>
In this example, the ows_Author and ows_Editor attributes pertain to lookup fields to another list in the database, representing the integer IDs of items in the UserInfo table and the actual values contained by these items within the table.

The following code example modifies the values of two different field values within two items in a list on the current site. The example uses an XmlDocument object to create XmlNode objects for parameters.

This example requires that a using (Visual C#) or Imports (Visual Basic) directive be included for the System.Xml namespace.

Web_Reference_Folder.Lists listService = new Web_Reference_Folder.Lists();
listService.Credentials= System.Net.CredentialCache.DefaultCredentials;

string strBatch = "<Method ID='1' Cmd='Update'>" + 
    "<Field Name='ID'>4</Field>" +
    "<Field Name='Field_Number'>999</Field></Method>" +
    "<Method ID='2' Cmd='Update'><Field Name='ID' >6</Field>" +
    "<Field Name='Field_DateTime'>
        2003-11-11T09:15:30Z</Field></Method>"; 

XmlDocument xmlDoc = new System.Xml.XmlDocument();

System.Xml.XmlElement elBatch = xmlDoc.CreateElement("Batch");

elBatch.SetAttribute("OnError","Continue");
elBatch.SetAttribute("ListVersion","1");
elBatch.SetAttribute("ViewName",
    "0d7fcacd-1d7c-45bc-bcfc-6d7f7d2eeb40");

elBatch.InnerXml = strBatch;

XmlNode ndReturn = listService.UpdateListItems("List_Name", elBatch);

MessageBox.Show(ndReturn.OuterXml);
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Can we use different identifier instead of ID?
For example:

I want to update list item with unique title "Test123"
is that possible?

batch =
"<Batch OnError=\"Continue\"> \
<Method ID=\"1\" Cmd=\"Update\"> \
<Field Name=\"EnableTeamPhoto\">1</Field> \
<Field Name=\"Title\">Test123</Field> \
</Method> \
</Batch>";
Mapping for column names in different languages
Hi, $0$0 $0 $0I am trying to use columns such as Efternamn (last name) in swedish version of Sharepoint.  Can you tell me is there any mapping defined for columns in english and other languages. And Where can i find such mapping?$0
cannot delete a file using this webservice APi
I am using this code in my websservice call
      <soap:UpdateListItems>
         <soap:listName>versionedlibrary</soap:listName>
         <soap:updates>
          <Batch OnError="Continue" >
             <Method ID="1" Cmd="Delete">
                 <Field Name='ID'>2</Field>
            </Method>
          </Batch>
         </soap:updates>
      </soap:UpdateListItems>

But getting this error -
    <ErrorText>Invalid file name.
The file name you specified could not be used.  It may be the name of an existing file or directory, or you may not have permission to access the file.</ErrorText>
 Can some one please help me with this ??

Wont work for html tags?
i am trying to use the same method to upadata an announcements item in my sharepoint server 2007 site$0 for some reason the following line$0 $0 batchElement.InnerXml = "<Method ID='1' Cmd='New'>" +$0 "<Field Name='Title'> " + txtSearch.Text.Trim() + " </Field>" +$0 "<Field Name='Body'>tt<html><head></head><body>123 12 3123 12312 312</body></html>tt</Field></Method>";$0 $0 only stores the title and stores tt in the body..$0 $0 anyway to bypass this? $0$0 $0 $0replied back to this thread.. i had to do some html encoding.$0 $0Works like a charm$0
expire not working?
Trying to get announcements to expire after 5 days.  CAML code im using is
<Field Name='Expires'>" + System.DateTime.Now.AddDays(5).ToString("yyyy-MM-dd") + "</Field>"

I see one blog is saying use ISO8601 format.  Can anyone provide an example?
Full code example of using this for batch updates
http://the-simple-programmer.blogspot.com/2010/11/render-content-type-fields-as-form-in.html
Update Document Name
Hi,

I am trying to update the document's name in a Document Library using Lists.UpdateListItems method,but it seems document name is not getting updated as we can do it using browser interface...

Here is the sample query that i am using..

strBatch = "<Method ID='1' Cmd='Update'>" +
"<Field Name='ID'>1</Field>" +
"<Field Name='Title'>Updated Title11</Field>" +
"<Field Name='BaseName'>Name</Field>" +
"</Method>";

Any guidance on this would be of great help ...



Reply from ijabit:
You can update the document's name by including the "FileRef" field as shown in the "Update a Document" section above. For some odd reason you have to specify the URL of the file to be renamed which can be a relative URL starting with a "/" if you want. Then you can specify the "BaseName" field which will update the filename. Be sure not to include the extension since it puts that in for you and you can only change the "BaseName". I would recommend also updating the title since the default title is the same as the old filename.
<Field Name="FileRef">http://Server/[sites/][Site/]Shared Documents/File</Field> OR <Field Name="FileRef">/Shared Documents/File</Field>
<Field Name="BaseName">NewName</Field>
<Field Name="Title">NewName</Field>

How to: Create an item (update/delte) inside a list folder
If you want to create an item list directly in a list folder you can use the following query:

<Method ID='1' Cmd='New'>
<Field Name='FSObjType'>0</Field>
<Field Name='BaseName'>MyFolderName/MyItemTitle</Field>
<Field Name='Title'>MyItemTitle</Field>
<Field Name='MyFirstFieldName'>MyFirstFieldValue</Field>
<Field Name='MySecondFieldName'>MySecondFieldValue</Field>
.
.
.
<Field Name='MyLastFieldName'>MyThirdFieldValue</Field>
</Method>
    
Note: The folder must be created before the executing this query.
    
    

Another alternative is to use the Rootfolder attribute of the Batch element to specify the working folder of the query as following:

<Batch OnError="Continue" RootFolder="/Lists/MyListName/MyFolderName">

<Method ID='1' Cmd='New'>
<Field Name='Title'>MyItemTitle</Field>
<Field Name='MyFirstFieldName'>MyFirstFieldValue</Field>
<Field Name='MySecondFieldName'>MySecondFieldValue</Field>
.
.
.
<Field Name='MyLastFieldName'>MyThirdFieldValue</Field>
</Method>
</Batch>
    
     
    
Hope with me that Microsoft improves thier documentation :)
calling web service in java

does anyone has any idea to call this UpdateListItem from Java?

[tfl - 21 10 09] Hi - and thanks for your post. You should post questions like this to the MSDN Forums at http://forums.microsoft.com/msdn or the MSDN Newsgroups at http://www.microsoft.com/communities/newsgroups/en-us/. You are much more likely get a quicker response using the forums than through the Community Content. For specific help about:
Visual Studio : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.vstudio%2C&
SQL Server : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.sqlserver%2C&
.NET Framework : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.dotnet.framework
PowerShell : http://groups.google.com/group/microsoft.public.windows.powershell/topics?pli=1
All Public : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public%2C&
Incorrect end-tag in first example

In the first example of showing how to modify column values for two specified items.


<Batch OnError="Continue" ListVersion="1"
ViewName="270C0508-A54F-4387-8AD0-49686D685EB2">
<Method ID="1" Cmd="Update">
<Field Name="ID">4<Field>
<Field Name="Field_Name">Value</Field>
</Method>
<Method ID="2" Cmd="Update">
<Field Name="ID" >6</Field>
<Field Name="Field_Name">Value</Field>
</Method>
</Batch>


<Field Name="ID">4<Field>
Should be
<Field Name="ID">4</Field>

If you copy and paste above example into your code without correct it first. You will get Error 400.


James Tsai | MCPD | SharePoint Consultant
My SharePoint Blog www.jamestsai.net/blog
Reference to web service URL

For some reason I could not get this to work unless I added a reference to the web service URL. Maybe I was missing a using statement or something. I ended up adding the one line below the web service setup.

Web_Reference_Folder.Lists listService = new Web_Reference_Folder.Lists();
listService.Credentials= System.Net.CredentialCache.DefaultCredentials;
listService.Url = "http://<Site>/_vti_bin/Lists.asmx";


I don't know why that was required, but I suppose that is a question for the forums. Also, if you are writing to a list then why would you care about the view? I think the following line is not required for this scenario.

elBatch.SetAttribute("ViewName",
"0d7fcacd-1d7c-45bc-bcfc-6d7f7d2eeb40");

Updating multi value fields using the UpdateListItems method
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/
Beware - Capitalization in your caml matters

I was trying to create a new folder and kept getting a SoapException stating:

Attribute ID or Cmd of element Method in parameter updates is missing or invalid

The problem was that I didnt see that my capitalization was wrong. Follow the examples listed on this MSDN page carefully and remember that XML is case-sensitive.

Incorrect code snippet:

strBatch = "<Method id='1' Cmd='New'>" + _
                "<Field Name='ID'>New</Field>" + _
                "<Field Name='FSObjType'>1</Field>" + _
                "<Field Name='BaseName'>NewFolderName</Field>" + _
            "</Method>"

Corrected code snippet (note the capitalized Method ID):

strBatch = "<Method ID='1' Cmd='New'>" + _
                "<Field Name='ID'>New</Field>" + _
                "<Field Name='FSObjType'>1</Field>" + _
                "<Field Name='BaseName'>NewFolderName</Field>" + _
            "</Method>"