Share via


Gewusst wie: Aktualisieren von Listenelementen

Letzte Änderung: Mittwoch, 7. Juli 2010

Gilt für: SharePoint Foundation 2010

In dieser Programmieraufgabe wird die UpdateListItems-Methode des Lists-Webdiensts zum Aktualisieren von Elementen in einer Liste über eine Microsoft Windows Forms-Anwendung verwendet.

Verwenden Sie ein XmlElement-Objekt zum Erstellen eines Batch-Elements in Collaborative Application Markup Language (CAML), das mehrere Method-Elemente enthalten kann, und verwenden Sie die UpdateListItems(String, XmlNode)-Methode zum Weiterleiten der Methoden und Aktualisieren der Elemente.

HinweisHinweis

Die Anzahl der Listenelemente, die Sie über die UpdateListItems-Methode in einem einzelnen Batch ändern können, ist auf 160 begrenzt.

Das Cmd-Attribut jedes Method-Elements bestimmt den Vorgang, der für ein Element durchgeführt wird, indem einer der folgenden Werte angegeben wird:

  • Delete - Ein Element löschen.

  • New - Ein Element erstellen.

  • Update - Ein Element ändern.

  • Move - Ein Element verschieben.

Verfahren

Bevor Sie beginnen, erstellen Sie eine Windows Forms-Anwendung in Microsoft Visual Studio. Informationen zum Festlegen eines Webverweises auf einen SharePoint Foundation-Webdienst finden Sie unter Richtlinien für Webdienste.

So fügen Sie Code zum Aktualisieren von Listenelementen hinzu

  1. Öffnen Sie Form1 in der Entwurfsansicht, öffnen Sie Toolbox, und ziehen Sie dann ein Button-Steuerelement in das Formular.

  2. Doppelklicken Sie auf das Button-Steuerelement, um den Code-Editor anzuzeigen, und fügen Sie dann die folgenden Codezeilen dem Button1_Click-Ereignishandler hinzu.

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

    Beim Weiterleiten der UpdateListItems-Methode tritt unbemerkt ein Fehler auf, wenn ein angegebenes Element nicht vorhanden ist. Die ID für ein gelöschtes Element wird nach einem Löschvorgang beibehalten. Daher wird im Beispiel das fünfte Element in der Liste gelöscht, die Zahl 5 wird jedoch nicht als ID für ein anderes Element neu zugewiesen.

  3. Klicken Sie im Menü Debuggen auf Starten, oder drücken Sie F5, um das Formular zu testen.