WEB服务更新列表项

http://msdn.microsoft.com/zh-cn/library/ms440289.aspx#

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

使用 System.Xml.XmlElement 对象以协作应用程序标记语言 (CAML) 创建 Batch 元素,该元素可包含多个 Method 元素,并使用 UpdateListItems 方法来发布方法和更新项目。每个 Method 元素的 Cmd 属性通过指定下列值之一确定对项目执行的操作:

  • Delete -- 删除项。

  • New -- 创建项。

  • Update -- 修改项。

过程

在开始之前,如在 Visual Studio 中以编程方式自定义 SharePoint 网站入门中所述创建 Windows 应用程序。有关设置对 Windows SharePoint Services Web 服务的 Web 引用的信息,请参阅 Web 服务指南

 

 

C#
/*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);
}
posted @ 2009-01-15 08:58  浪漫稻草人  阅读(212)  评论(0)    收藏  举报