Partilhar via


How to: Return Lists

Applies to: SharePoint Foundation 2010

This programming task shows how to create a simple Windows form that uses the GetListCollection method of the Lists Web service to retrieve the collection of lists and display their names.

Procedures

Before you begin, create a Windows Forms application in Microsoft Visual Studio. For information about setting a Web reference to a Microsoft SharePoint Foundation Web service, see Web Service Guidelines.

To add code to display the collection of lists

  1. Open Form1 in Design view, open the Toolbox, and then drag a button control and a text box control onto the form.

  2. Resize the text box to fill the form under the button.

  3. Right-click the text box control, click Properties, and then set the Multiline property to True and the ScrollBars property to Vertical.

  4. Double-click the Button control to display the Code Editor, and add the following lines of code to the Button1_Click event handler.

    'Declare and initialize a variable for the Lists Web service.
    Dim myservice As New Web_Reference.Lists()
    
    'Authenticate the current user by passing their default 
    'credentials to the Web service from the system credential 
    'cache. 
    myservice.Credentials = System.Net.CredentialCache.DefaultCredentials
    
    'Set the Url property of the service for the path to a subsite. 
    'Not setting this property will return the lists in the root 
    'Web site
    myservice.Url = "http://Server_Name/Subsite_Name/_vti_bin/Lists.asmx"
    
    'Declare an XmlNode object and initialize it with the XML 
    'response from the GetListCollection method. 
    Dim node As System.Xml.XmlNode = myservice.GetListCollection()
    
    'Loop through XML response and parse out the value of the
    'Title attribute for each list. 
    Dim xmlnode As System.Xml.XmlNode
    For Each xmlnode In node
       textBox1.Text += xmlnode.Attributes("Title").Value + Environment.NewLine
    Next xmlnode
    
    /*Declare and initialize a variable for the Lists Web service.*/
    Web_Reference.Lists myservice = new Web_Reference.Lists();
    
    /*Authenticate the current user by passing their default 
    credentials to the Web service from the system credential 
    cache. */
    myservice.Credentials = 
       System.Net.CredentialCache.DefaultCredentials;
    
    /*Set the Url property of the service for the path to a subsite. 
    Not setting this property will return the lists in the root Web site.*/
    myservice.Url = 
    "http://Server_Name/Subsite_Name/_vti_bin/Lists.asmx";
    
    /*Declare an XmlNode object and initialize it with the XML 
    response from the GetListCollection method. */
    System.Xml.XmlNode node = myservice.GetListCollection();
    
    /*Loop through XML response and parse out the value of the
    Title attribute for each list. */
    foreach(System.Xml.XmlNode xmlnode in node) 
    {
       textBox1.Text+=xmlnode.Attributes["Title"].Value + Environment.NewLine;
    }
    
  5. On the Debug menu, click Start Debugging to test the form. Click the button in the form to display the lists in your SharePoint site.