Share via


Procédure : mettre à jour des éléments de liste

Dernière modification : mercredi 7 juillet 2010

S’applique à : SharePoint Foundation 2010

Cette tâche de programmation montre comment utiliser la méthode UpdateListItems du service Web Lists pour mettre à jour des éléments dans une liste via une application Microsoft Windows Forms.

Utilisez un objet XmlElement pour créer un élément Batch en langage CAML (Collaborative Application Markup Language) pouvant contenir plusieurs éléments Method, et utilisez la méthode UpdateListItems(String, XmlNode) pour publier les méthodes et mettre à jour les éléments.

Notes

Le nombre d’éléments de liste que vous pouvez modifier par le biais de la méthode UpdateListItems en un seul lot est limité à 160.

L’attribut Cmd de chaque élément Method détermine l’opération effectuée sur un élément en spécifiant l’une des valeurs suivantes :

  • Delete -- Supprimer un élément.

  • New -- Créer un élément.

  • Update -- Modifier un élément.

  • Move -- Déplacer un élément.

Procédures

Avant de commencer, créez une application Windows Forms dans Microsoft Visual Studio. Pour obtenir des informations sur la définition d’une référence Web vers un service Web SharePoint Foundation, voir Conseils d'utilisation des services Web.

Pour ajouter du code pour mettre à jour des éléments de liste

  1. Ouvrez Form1 en mode Création, ouvrez la Boîte à outils, puis faites glisser un contrôle Button sur le formulaire.

  2. Double-cliquez sur le contrôle Button pour afficher l’Éditeur de code, puis ajoutez les lignes de code suivantes au gestionnaire d’événements 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);
    }
    

    Notes

    La publication de la méthode UpdateListItems en mode silencieux échoue si un élément spécifié n’existe pas. L’ID d’un élément supprimé est conservé après une opération de suppression. Par conséquent, l’exemple supprime le cinquième élément de la liste, mais 5 n’est pas réaffecté comme ID d’un autre élément.

  3. Dans le menu Débogage, cliquez sur Démarrer le débogage ou appuyez sur F5 pour tester le formulaire.