リストを返す

最終更新日: 2010年8月3日

適用対象: SharePoint Foundation 2010

ここでは、Lists Web サービスの GetListCollection メソッドを使用してリストのコレクションを取得しリスト名を表示する、単純な Windows フォームを作成する方法について説明します。

手順

始める前に、Microsoft Visual Studio で Windows Forms アプリケーションを作成します。Microsoft SharePoint Foundation の Web サービスへの Web 参照を設定する方法の詳細については、「Windows SharePoint Services 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 サイト内のリストが表示されます。