Share via


Gewusst wie: Zurückgeben von Listenelementen

Letzte Änderung: Mittwoch, 7. Juli 2010

Gilt für: SharePoint Foundation 2010

In dieser Programmieraufgabe wird das Erstellen eines Windows-Formulars gezeigt, das die GetListItems-Methode des Listen-Webdiensts zum Zurückgeben von Elementen aus einer Liste verwendet.

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 Anzeigen der Auflistung mit Listenelenenten hinzu

  1. Nachdem Sie eine Windows Forms-Anwendung erstellt und einen Webverweis hinzugefügt haben, öffnen Sie Form1 in der Entwurfsansicht, öffnen Sie Toolbox, und ziehen Sie dann ein Label-Steuerelement und ein Button-Steuerelement in das Formular.

  2. Klicken Sie mit der rechten Maustaste auf das Steuerelement Label, klicken Sie auf Eigenschaften, und löschen Sie dann den Wert Label1 aus der Text-Eigenschaft des Steuerelements.

  3. Doppelklicken Sie auf die Schaltfläche, 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 Web_Reference.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://Server_Name/Subsite_Name/_vti_bin/Lists.asmx"
    
    'Instantiate an XmlDocument object.
    Dim xmlDoc As New System.Xml.XmlDocument()
    
    'Assign values to the string parameters of the GetListItems method, 
    'using GUIDs for the listName and viewName variables. For listName, 
    'using the list display name will also work, but using the list GUID 
    'is recommended. For viewName, only the view GUID can be used. 
    'Using an empty string for viewName causes the default view 
    'to be used.
    Dim listName As String = "{17991794-81BB-494F-9910-CFBF1093A7CF}"
    Dim viewName As String = "{7137FFF8-48FF-4C69-8C76-0E3BBD1EA7F9}"
    Dim rowLimit As String = "150"
    
    'Use the CreateElement method of the document object to create 
    'elements for the parameters that use XML.
    Dim query As System.Xml.XmlElement = xmlDoc.CreateElement("Query")
    Dim viewFields As System.Xml.XmlElement = xmlDoc.CreateElement("ViewFields")
    Dim queryOptions As System.Xml.XmlElement = xmlDoc.CreateElement("QueryOptions")
    
    'To specify values for the parameter elements (optional), assign CAML 
    ' fragments to the InnerXml property of each element.
    query.InnerXml = "<Where><Gt><FieldRef Name=""ID"" />" + 
    "<Value Type=""Counter"">3</Value></Gt></Where>"
    viewFields.InnerXml = "<FieldRef Name=""Title"" />"
    queryOptions.InnerXml = ""
    
    'Declare an XmlNode object and initialize it with the XML response 
    'from the GetListItems method. The last parameter specifies 
    'the GUID of the Web site containing the list. Setting it to 
    'Nothing causes the Web site specified by the Url property 
    'to be used.
    Dim nodeListItems As System.Xml.XmlNode = listService.GetListItems(listName, viewName, query, viewFields, rowLimit, queryOptions, Nothing)
    
    'Loop through each node in the XML response and display each item.
    Dim listItem As System.Xml.XmlNode
    For Each listItem In  nodeListItems
       label1.Text += listItem.OuterXml
    Next listItem
    
    /*Declare and initialize a variable for the Lists Web service.*/
    Web_Reference.Lists listService = new Web_Reference.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://Server_Name/Subsite_Name/_vti_bin/Lists.asmx";
    
    /* Instantiate an XmlDocument object */
    System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
    
    /* Assign values to the string parameters of the GetListItems method, 
    using GUIDs for the listName and viewName variables. For listName, 
    using the list display name will also work, but using the list GUID 
    is recommended. For viewName, only the view GUID can be used. 
    Using an empty string for viewName causes the default view to be used.*/
    string listName = "{17991794-81BB-494F-9910-CFBF1093A7CF}";
    string viewName = "{7137FFF8-48FF-4C69-8C76-0E3BBD1EA7F9}";
    string rowLimit = "150";
    
    /*Use the CreateElement method of the document object to create 
    elements for the parameters that use XML.*/
    System.Xml.XmlElement query = xmlDoc.CreateElement("Query");
    System.Xml.XmlElement viewFields = 
        xmlDoc.CreateElement("ViewFields");
    System.Xml.XmlElement queryOptions = 
        xmlDoc.CreateElement("QueryOptions");
    
    /*To specify values for the parameter elements (optional), assign 
    CAML fragments to the InnerXml property of each element.*/
    query.InnerXml = "<Where><Gt><FieldRef Name=\"ID\" />" +
        "<Value Type=\"Counter\">3</Value></Gt></Where>";
    viewFields.InnerXml = "<FieldRef Name=\"Title\" />";
    queryOptions.InnerXml = "";
    
    /* Declare an XmlNode object and initialize it with the XML response 
    from the GetListItems method. The last parameter specifies the GUID 
    of the Web site containing the list. Setting it to null causes the 
    Web site specified by the Url property to be used.*/
    System.Xml.XmlNode nodeListItems = 
        listService.GetListItems
        (listName, viewName,query,viewFields,rowLimit,queryOptions,null);
    
    /*Loop through each node in the XML response and display each item.*/
    foreach (System.Xml.XmlNode listItem in nodeListItems)
    {
        label1.Text += listItem.OuterXml;
    }
    
    HinweisHinweis

    Sie müssen eigene Werte für die Variablen listName, viewName und rowLimit angeben, die im Beispiel verwendet werden.

  4. Klicken Sie im Menü Debuggen auf Starten, um das Formular zu testen. Klicken Sie auf die Schaltfläche im Formular, um die Listenelemente aus der angegebenen Liste anzuzeigen.