Share via


Updating List Items

This programming task shows how to use the UpdateListItems method of the Lists Web service to update items in a list.

To create a new Windows Application project  

  • Start Visual Studio .NET.
  • On the File menu, point to New, and then click Project.
  • In the New Project dialog box, click Visual C# Projects or Visual Basic Projects, and then select the Windows Application template.
  • Type "UpdateListItems" as the name, specify the location for the project files, and then click OK.

To add a reference to the Lists Web Service  

  • In Solution Explorer, right-click Web References, and then click Add Web Reference.

  • In the Add Web Reference dialog box, enter the URL for the Lists Web Service on the server running Microsoft Windows SharePoint Services:

    http://Server_Name/_vti_bin/Lists.asmx

    and then click Add Reference.

To add code to update list items  

Use the XmlElement class to contain a Batch element in Collaborative Application Markup Language (CAML), and then use the UpdateListItems method to update the items.

  • Open Form1 in Design view, display 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.
    Server_Name.Lists listService = new Server_Name.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://Server_Name/Site_Name/_vti_bin/Lists.asmx";
    
    // Create an XmlDocument object and construct a Batch element and its attributes.
    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","{7137FFF8-48FF-4C69-8C76-0E3BBD1EA7F9}");
    
    /* Specify methods for the batch post using CAML. In each method include
    the ID of the item to update and the value to place in the specified column.*/
    batchElement.InnerXml = "<Method ID='1' Cmd='Update'><Field Name='ID'>6</Field><Field Name='Title'>sixth</Field></Method><Method ID='2' Cmd='Update'><Field Name='ID'>7</Field><Field Name='Title'>seventh</Field></Method>";
    
    // Update list items.
    listService.UpdateListItems("{17991794-81BB-494F-9910-CFBF1093A7CF}", batchElement);
    
  • On the Debug menu, click Start to test the form. Click Button1 to display the list items from the specified list.