How to: Update List Items
2012-03-26 17:52
295 查看
SharePoint 2010
Other Versions
0 out of 20 rated this helpful Rate this topic
Published: May 2010
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 Forms application.
Use an XmlElement object to create a Batch element in Collaborative Application Markup Language (CAML) that can contain multiple Method elements, and use the UpdateListItems(String, XmlNode) 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.
Move -- Move an item.
Procedures
Before you begin, create a Windows Forms application in Microsoft Visual Studio. For information about setting a Web reference to a SharePoint Foundation Web service, see Web Service Guidelines.
Double-click the Button control to display the Code Editor, and then add the following lines of code to theButton1_Click event handler.
VB
C#
On the Debug menu, click Start Debugging, or press F5, to test the form.
http://msdn.microsoft.com/en-us/library/ms440289.aspx
Other Versions
0 out of 20 rated this helpful Rate this topic
Published: May 2010
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 Forms application.
Use an XmlElement object to create a Batch element in Collaborative Application Markup Language (CAML) that can contain multiple Method elements, and use the UpdateListItems(String, XmlNode) method to post the methods and update items.
Note |
---|
The number of list items that you can modify through the UpdateListItems method in a single batch is limited to 160. |
Delete -- Delete an item.
New -- Create an item.
Update -- Modify an item.
Move -- Move an item.
Procedures
Before you begin, create a Windows Forms application in Microsoft Visual Studio. For information about setting a Web reference to a SharePoint Foundation 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 Button control to display the Code Editor, and then add the following lines of code to theButton1_Click event handler.
VB
'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)
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); }
Note |
---|
Posting the UpdateListItems method silently fails if a specified item does not exist. The identifier (ID) for a deleted item is maintained after a delete operation. Consequently, the example deletes the fifth item in the list, but number 5 is not reassigned as the ID for another item. |
http://msdn.microsoft.com/en-us/library/ms440289.aspx
相关文章推荐
- [导入]使用Sharepoint的Lists Web Service调用UpdateListItems方法"增/删/改"List Items
- 使用WSS的Lists.UpdateListItems()方法之被截断的CAML
- Moss2007 UpdateListItems方法报错 ErrorCode 0x81020014
- JavaScript 调用sharepoint内置webservice(UpdateListItems和GetListItem())
- 采用Lists.UpdateListItems方法更新列表项各种类型值的写法
- Break the list items Inheritance permission and custom the permission with ItemAdded Event
- 复制单链表成为新的链表,然后return the number of items in the new list
- HowTo: ListView, Adapter, getView and different list items’ layouts in one ListView
- ListControl.AppendDataBoundItems 属性
- SQL Error:An aggregate may not appear in the set list of an UPDATE statement.
- How to: Filter List Views on the UI Specific Level
- Using jQuery and SPServices to Display List Items
- Display selected items of a checkboxlist
- Update your urlpatterns to be a list of django.conf.urls.url() instances instead. Django 1.10. Updat
- How to list the properties of a JavaScript object
- update list get()方法
- 如何避免TStringList里的Items雷同
- HowTo: ListView, Adapter, getView and different list items’ layouts in one ListView
- Sharepoint学习笔记---SPList--清除List下的items与folders
- 【翻译】ASP.NET MVC:转化业务对象到Select List Items上