共用方式為


How to: Return Lists

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

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 application as described in Getting Started with Programmatically Customizing a SharePoint Web Site in Visual Studio. For information about setting a Web reference to a Windows SharePoint Services 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 Label control and a Button control on to the form.

  2. Right-click the Label1 control, click Properties, and then delete the value Label1 from the Text property of the control.

  3. Double-click the Button1 control to display the Code Editor, and add the following lines of code to the 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
    sitelistService.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
       label1.Text += xmlnode.Attributes("Title").Value + ControlChars.Lf
    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.*/
    listService.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) 
    {
       label1.Text+=xmlnode.Attributes["Title"].Value + "\n";
    }
    
  4. On the Debug menu, click Start to test the form. Click Button1 to display the lists in your SharePoint site.