如何:更新列表项

上次修改时间: 2010年7月7日

适用范围: SharePoint Foundation 2010

此编程任务演示如何使用 Lists Web 服务的 UpdateListItems 方法通过 Microsoft Windows 窗体应用程序来更新列表中的项。

使用 System.Xml.XmlElement 对象以协作应用程序标记语言 (CAML) 创建 Batch 元素,该元素可包含多个 Method 元素,并使用 UpdateListItems(String, XmlNode) 方法来发布方法和更新项目。

备注

在单一批次中可通过 UpdateListItems 方法修改的列表项的数量限制为 160。

每个 Method 元素的 Cmd 属性通过指定下列值之一确定对项目执行的操作:

  • Delete -- 删除项。

  • New -- 创建项。

  • Update -- 修改项。

  • Move -- 移动项。

过程

在开始之前,使用 Microsoft Visual Studio 创建一个 Windows 窗体应用程序。有关设置指向 SharePoint Foundation Web 服务的 Web 引用的信息,请参阅 Web 服务指南

添加代码以更新列表项

  1. 在设计视图中打开"Form1",打开"工具箱",然后将 Button 控件拖动到表单上。

  2. 双击 Button 控件以显示"代码编辑器",然后将下面的代码行添加到 Button1_Click 事件处理程序中。

    '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);
    }
    

    备注

    如果指定的项不存在,则发布 UpdateListItems 方法时会失败,并且不会出现任何提示。在删除操作后会保留已删除项目的标识符 (ID)。因此,此示例删除列表中的第五个项目,但不会将数字 5 作为 ID 重新分配给另一项目。

  3. 在"调试"菜单上,单击"启动调试",或按 F5 以测试表单。