リスト アイテムを更新する

最終更新日: 2010年7月7日

適用対象: SharePoint Foundation 2010

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

Collaborative Application Markup Language (CAML)Batch 要素を作成するには、XmlElement オブジェクトを使用します。Batch 要素には複数の Method 要素を含めることができます。メソッドをポストして項目を更新するには、UpdateListItems(String, XmlNode) メソッドを使用します。

注意

1 つのバッチで UpdateListItems メソッドを使用して変更できるリスト項目数は、160 に制限されています。

Method 要素の Cmd 属性に次のいずれかの値を指定することによって、項目に対して実行される操作が決まります。

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

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

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

  • Move -- 項目を移動します。

手順

始める前に、Microsoft Visual Studio で Windows Forms アプリケーションを作成します。SharePoint Foundation の Web サービスへの Web 参照を設定する方法の詳細については、「Windows SharePoint Services Web サービスの紹介」を参照してください。

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

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

  2. [Button] コントロールをダブルクリックしてコード エディターを開き、次のコード行を Button1_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 キーを押して、フォームをテストします。