如何:返回列表

上次修改时间: 2010年8月3日

适用范围: SharePoint Foundation 2010

此编程任务演示如何创建一个简单的 Windows 窗体,该窗体使用 Lists Web 服务的 GetListCollection 方法来检索列表的集合并显示其名称。

过程

在开始之前,使用 Microsoft Visual Studio 创建一个 Windows 窗体应用程序。有关设置指向 Microsoft SharePoint Foundation Web 服务的 Web 引用的信息,请参阅 Web 服务指南

添加代码以显示列表集合

  1. 在设计视图中打开"Form1",打开"工具箱",然后将一个按钮控件和一个文本框控件拖动到窗体上。

  2. 调整文本框大小以便将窗体放在按钮下面。

  3. 右键单击文本框控件,单击"属性",然后将 Multiline 属性设置为 True,将 ScrollBars 属性设置为 Vertical。

  4. 双击"Button"控件以显示"代码编辑器",并将下面的代码行添加到 Button1_Click 事件处理程序。

    '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. 在"调试"菜单上,单击"启动调试"以测试表单。单击表单上的按钮以显示 SharePoint 网站中的列表。