このプログラミング タスクでは、Lists Web サービスの UpdateListItems メソッドを使用して、Microsoft Windows アプリケーションからリストの項目を更新する方法を示します。

Collaborative Application Markup Language (CAML)Batch 要素を作成するには、System.Xml.XmlElement オブジェクトを使用します。Batch 要素には複数の Method 要素を含めることができます。メソッドをポストして項目を更新するには、UpdateListItems メソッドを使用します。各 Method 要素の Cmd 属性に次のいずれかの値を指定することによって、項目に実行される操作が決まります。

  • Delete -- 項目を削除します。

  • New -- 項目を作成します。

  • Update -- 項目を変更します。

手順

始める前に、「Visual Studio で SharePoint Web サイトのプログラムによるカスタマイズ作業を開始する」で示された Windows アプリケーションを作成します。Windows SharePoint Services の Web サービスへの Web 参照を設定する方法については、「Windows SharePoint Services Web サービスの紹介」を参照してください。

注意

以下の例では、"MyList" というリストに少なくともいくつかの項目があることを想定しています。

リスト項目を更新するコードを追加するには

  1. デザイン ビューで [Form1] を開き、[ツールボックス] を開いて、[ボタン] コントロールをフォームにドラッグします。

  2. [ボタン 1] コントロールをダブルクリックしてコード エディタを開き、次のコード行を Click イベント ハンドラに追加します。

    'Declare and initialize a variable for the Lists Web service.
    Dim listService As New sitesWebServiceLists.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://MyServer/sites/MySiteCollection/_vti_bin/Lists.asmx"
    
    'Get Name attribute values (GUIDs) for list and view. 
    Dim ndListView As System.Xml.XmlNode = listService.GetListAndView("MyList", "")
    Dim strListID As String = ndListView.ChildNodes(0).Attributes("Name").Value
    Dim strViewID As String = ndListView.ChildNodes(1).Attributes("Name").Value
    
    'Create an XmlDocument object and construct a Batch element and its 'attributes. Note that an empty ViewName parameter causes the method 'to use the default view. 
    Dim doc As New System.Xml.XmlDocument()
    Dim batchElement As System.Xml.XmlElement = doc.CreateElement("Batch")
    batchElement.SetAttribute("OnError", "Continue")
    batchElement.SetAttribute("ListVersion", "1")
    batchElement.SetAttribute("ViewName", strViewID)
    
    'Specify methods for the batch post using CAML. To update or delete, 'specify the ID of the item, and to update or add, specify 
    'the value to place in the specified columns.
    batchElement.InnerXml = "<Method ID='1' Cmd='Update'>" +
       "<Field Name='ID'>6</Field>" +
       "<Field Name='Title'>Modified sixth item</Field></Method>" +
       "<Method ID='2' Cmd='Update'><Field Name='ID'>7</Field>" +
       "<Field Name='Title'>Modified seventh item</Field></Method>" +
       "<Method ID='3' Cmd='Delete'><Field Name='ID'>5</Field>" +
       "</Method><Method ID='4' Cmd='New'>" +
       "<Field Name='Title'>Added item</Field></Method>"
    
    'Update list items. This example uses the list GUID, which is recommended, but the list display name will also work.
    listService.UpdateListItems(strListID, batchElement)
    
    /*Declare and initialize a variable for the Lists Web service.*/
    sitesWebServiceLists.Lists listService = new sitesWebServiceLists.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://MyServer/sites/MySiteCollection/_vti_bin/Lists.asmx";
    
    /*Get Name attribute values (GUIDs) for list and view. */
    System.Xml.XmlNode ndListView = listService.GetListAndView("MyList", "");
    string strListID = ndListView.ChildNodes[0].Attributes["Name"].Value;
    string strViewID = ndListView.ChildNodes[1].Attributes["Name"].Value;
    
    /*Create an XmlDocument object and construct a Batch element and its attributes. Note that an empty ViewName parameter causes the method to use the default view. */
    System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
    System.Xml.XmlElement batchElement = doc.CreateElement("Batch");
    batchElement.SetAttribute("OnError", "Continue");
    batchElement.SetAttribute("ListVersion", "1");
    batchElement.SetAttribute("ViewName", strViewID);
    
    /*Specify methods for the batch post using CAML. To update or delete, specify the ID of the item, and to update or add, specify 
    the value to place in the specified column.*/
    batchElement.InnerXml = "<Method ID='1' Cmd='Update'>" +
       "<Field Name='ID'>6</Field>" +
       "<Field Name='Title'>Modified sixth item</Field></Method>" +
       "<Method ID='2' Cmd='Update'><Field Name='ID'>7</Field>" +
       "<Field Name='Title'>Modified seventh item</Field></Method>" +
       "<Method ID='3' Cmd='Delete'><Field Name='ID'>5</Field>" +
       "</Method><Method ID='4' Cmd='New'>" +
       "<Field Name='Title'>Added item</Field></Method>";
    
    /*Update list items. This example uses the list GUID, which is recommended, but the list display name will also work.*/
    try
    {
       listService.UpdateListItems(strListID, batchElement);
    }
    catch (SoapServerException ex)
    {
       MessageBox.Show(ex.Message);
    }
    

    注意

    指定した項目が存在しない場合、UpdateListItems メソッドのポストは通知なく失敗します。削除した項目の ID は、削除操作後も保持されます。したがって、この例では 5 番目の項目を削除しますが、5 という番号が別の項目の ID として再度割り当てられることはありません。

  3. [デバッグ] メニューの [開始] をクリックするか、F5 キーを押して、フォームをテストします。