[方法] リスト アイテムの作成、更新、削除を行う

最終更新日: 2011年4月29日

適用対象: SharePoint Foundation 2010

この記事の内容
リスト アイテムを作成する
リスト アイテムを更新する
リスト アイテムを削除する

SharePoint Online で使用可能

クライアント オブジェクト モデルでは、サーバー オブジェクト モデルとほぼ同じように、リスト アイテムを作成、更新、または削除できます。リスト アイテム オブジェクトを作成するには、オブジェクトを作成し、プロパティを設定し、そのオブジェクトを更新します。リスト アイテム オブジェクトを変更または削除するには、ListItemCollection クラス (JavaScript: ListItemCollection) の GetById() メソッド (JavaScript: getById(id)) を使用してオブジェクトを返し、プロパティを設定してこのメソッドによって返されるオブジェクトの更新を呼び出すか、オブジェクト自体の削除のメソッドを呼び出します。サーバー オブジェクト モデルと異なるのは、クライアント オブジェクト モデルのそれぞれの操作が ExecuteQuery() または ExecuteQueryAsync(ClientRequestSucceededEventHandler, ClientRequestFailedEventHandler) (JavaScript: executeQueryAsync(succeededCallback, failedCallback)) への呼び出しで終了し、サーバー上に変更を反映させる必要があるという点です。

リスト アイテムを作成する

リスト アイテムを作成するには、ListItemCreationInformation オブジェクト (JavaScript: ListItemCreationInformation) を作成し、プロパティを設定して、それをパラメーターとして List クラス (JavaScript: List) の AddItem(ListItemCreationInformation) メソッド (JavaScript: addItem(parameters)) に渡します。次の例で示すように、このメソッドによって返されるリスト アイテム オブジェクトのプロパティを設定し、Update() (JavaScript: update()) を呼び出します。

using System;
using Microsoft.SharePoint.Client;
using SP = Microsoft.SharePoint.Client;

namespace Microsoft.SDK.SharePointServices.Samples
{
    class CreateListItem
    {
        static void Main()
        {   
            string siteUrl = "http://MyServer/sites/MySiteCollection";

            ClientContext clientContext = new ClientContext(siteUrl);
            SP.List oList = clientContext.Web.Lists.GetByTitle("Announcements");

            ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
            ListItem oListItem = oList.AddItem(itemCreateInfo);
            oListItem["Title"] = "My New Item!";
            oListItem["Body"] = "Hello World!";

            oListItem.Update();

            clientContext.ExecuteQuery(); 
        }
    }
}
Imports System
Imports Microsoft.SharePoint.Client
Imports SP = Microsoft.SharePoint.Client

Namespace Microsoft.SDK.SharePointServices.Samples
    Class CreateListItem

        Shared Sub Main ()

            Dim siteUrl As String = "http://MyServer/sites/MySiteCollection"

            Dim clientContext As New ClientContext(siteUrl)
            Dim oList As SP.List = clientContext.Web.Lists.GetByTitle("Announcements")

            Dim itemCreateInfo As New ListItemCreationInformation()
            Dim oListItem As ListItem = oList.AddItem(itemCreateInfo)
            oListItem("Title") = "My New Item!"
            oListItem("Body") = "Hello World!"

            oListItem.Update()

            clientContext.ExecuteQuery()

        End Sub
    End Class
End Namespace
var siteUrl = '/sites/MySiteCollection';

function createListItem() {

    var clientContext = new SP.ClientContext(siteUrl);
    var oList = clientContext.get_web().get_lists().getByTitle('Announcements');
        
    var itemCreateInfo = new SP.ListItemCreationInformation();
    this.oListItem = oList.addItem(itemCreateInfo);
        
    oListItem.set_item('Title', 'My New Item!');
    oListItem.set_item('Body', 'Hello World!');
        
    oListItem.update();

    clientContext.load(oListItem);
        
    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

function onQuerySucceeded() {

    alert('Item created: ' + oListItem.get_id());
}

function onQueryFailed(sender, args) {

    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

前の例では標準のリスト アイテムが作成されているため、ListItemCreationInformation オブジェクト (JavaScript: ListItemCreationInformation) が AddItem(ListItemCreationInformation) メソッド (JavaScript: addItem(parameters)) に渡される前に、そのオブジェクトのプロパティを設定する必要はありません。ただし、たとえば、コードで新しいフォルダーを作成する必要がある場合は、ListItemCreationInformation オブジェクト (JavaScript: ListItemCreationInformation) の UnderlyingObjectType プロパティ (JavaScript: underlyingObjectType) を Folder (JavaScript: folder) に設定する必要があります。

Microsoft SharePoint Foundation 2010 Silverlight オブジェクト モデルのコンテキスト内でリスト アイテム オブジェクトを作成する方法および例については、「Silverlight オブジェクト モデルを使用する」を参照してください。

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

ほとんどのリスト アイテム プロパティは、列インデクサーを使用して割り当てを作成し、Update() メソッド (JavaScript: update()) を呼び出すことで設定できます。変更は、ExecuteQuery() または ExecuteQueryAsync(ClientRequestSucceededEventHandler, ClientRequestFailedEventHandler) (JavaScript: executeQueryAsync(succeededCallback, failedCallback)) を呼び出したときに反映されます。次の例では、Announcements リストの 3 番目のアイテムのタイトルを設定しています。

using System;
using Microsoft.SharePoint.Client;
using SP = Microsoft.SharePoint.Client;

namespace Microsoft.SDK.SharePointServices.Samples
{
    class UpdateListItem
    {
        static void Main()
        {   
            string siteUrl = "http://MyServer/sites/MySiteCollection";

            ClientContext clientContext = new ClientContext(siteUrl);
            SP.List oList = clientContext.Web.Lists.GetByTitle("Announcements");
            ListItem oListItem = oList.Items.GetById(3);

            oListItem["Title"] = "My Updated Title.";

            oListItem.Update();

            clientContext.ExecuteQuery(); 
        }
    }
}
Imports System
Imports Microsoft.SharePoint.Client
Imports SP = Microsoft.SharePoint.Client

Namespace Microsoft.SDK.SharePointServices.Samples
    Class UpdateListItem

        Shared Sub Main ()

            Dim siteUrl As String = "http://MyServer/sites/MySiteCollection"

            Dim clientContext As New ClientContext(siteUrl)
            Dim oList As List = clientContext.Web.Lists.GetByTitle("Announcements")
            Dim oListItem As ListItem = oList.Items.GetById(3)

            oListItem("Title") = "My Updated Title."

            oListItem.Update()

            clientContext.ExecuteQuery()

        End Sub
    End Class
End Namespace
var siteUrl = '/sites/MySiteCollection';

function updateListItem() {

    var clientContext = new SP.ClientContext(siteUrl);
    var oList = clientContext.get_web().get_lists().getByTitle('Announcements');

    this.oListItem = oList.getItemById(3);

    oListItem.set_item('Title', 'My Updated Title');

    oListItem.update();

    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

function onQuerySucceeded() {

    alert('Item updated!');
}

function onQueryFailed(sender, args) {

    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

リスト アイテムを削除する

リスト アイテムを削除するには、オブジェクトで DeleteObject() メソッド (JavaScript: deleteObject()) を呼び出します。次の例では、GetItemById() メソッド (JavaScript: getItemById(id)) を使用してリストの 2 番目のアイテムを返し、そのアイテムを削除しています。

SharePoint Foundation 2010 では、コレクション内のアイテムを削除しても、そのアイテムの整数の ID が保持されます。したがって、たとえば、リスト内の 2 番目のアイテムの ID が 2 にならない場合があります。存在しないアイテムに対して DeleteObject() メソッド (JavaScript: deleteObject()) が呼び出された場合は、ServerException が返されます。

using System;
using Microsoft.SharePoint.Client;
using SP = Microsoft.SharePoint.Client;

namespace Microsoft.SDK.SharePointServices.Samples
{
    class DeleteListItem
    {
        static void Main()
        {   
            string siteUrl = "http://MyServer/sites/MySiteCollection";

            ClientContext clientContext = new ClientContext(siteUrl);
            SP.List oList = clientContext.Web.Lists.GetByTitle("Announcements");
            ListItem oListItem = oList.GetItemById(2);

            oListItem.DeleteObject();

            clientContext.ExecuteQuery(); 
        }
    }
}
Imports System
Imports Microsoft.SharePoint.Client
Imports SP = Microsoft.SharePoint.Client

Namespace Microsoft.SDK.SharePointServices.Samples
    Class DeleteListItem

        Shared Sub Main ()

            Dim siteUrl As String = "http://MyServer/sites/MySiteCollection"

            Dim clientContext As New ClientContext(siteUrl)
            Dim oList As List = clientContext.Web.Lists.GetByTitle("Announcements")
            Dim oListItem As ListItem = oList.GetItemById(2)

            oListItem.DeleteObject()

            clientContext.ExecuteQuery()
        End Sub
    End Class
End Namespace
var siteUrl = '/sites/MySiteCollection';

function deleteListItem() {

    this.itemId = 2;

    var clientContext = new SP.ClientContext(siteUrl);
    var oList = clientContext.get_web().get_lists().getByTitle('Announcements');

    this.oListItem = oList.getItemById(itemId);

    oListItem.deleteObject();

    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

function onQuerySucceeded() {

    alert('Item deleted: ' + itemId);
}

function onQueryFailed(sender, args) {

    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

たとえば、削除操作によって更新された新しいアイテム数を取得する必要がある場合は、Update() メソッド (JavaScript: update()) への呼び出しを含めて、リストを更新します。また、クエリを実行する前に、リスト オブジェクト自体またはリスト オブジェクト上の ItemCount プロパティ (JavaScript: itemCount) のどちらかを読み込む必要があります。リスト アイテムの開始数と終了数の両方を取得する場合は、前の例に次の変更を加えて、2 つのクエリを実行し、アイテム数を 2 回返す必要があります。

using System;
using Microsoft.SharePoint.Client;
using SP = Microsoft.SharePoint.Client;

namespace Microsoft.SDK.SharePointServices.Samples
{
    class DeleteListItemDisplayCount
    {
        static void Main()
        {   
            string siteUrl = "http://MyServer/sites/MySiteCollection";

            ClientContext clientContext = new ClientContext(siteUrl);
            SP.List oList = clientContext.Web.Lists.GetByTitle("Announcements");

            clientContext.Load(oList,
                list => list.ItemCount);

            clientContext.ExecuteQuery();

            int startCount = oList.ItemCount;
            ListItem oListItem = oList.GetItemById(2);

            oListItem.DeleteObject();

            oList.Update();

            clientContext.Load(oList,
                list => list.ItemCount);

            clientContext.ExecuteQuery();

            int endCount = oList.ItemCount;

            Console.WriteLine("Start: {0}  End: {1}", startCount, endCount);
        }
    }
}
Imports System
Imports Microsoft.SharePoint.Client
Imports SP = Microsoft.SharePoint.Client

Namespace Microsoft.SDK.SharePointServices.Samples
    Class DeleteListItemDisplayCount

        Shared Sub Main ()

            Dim siteUrl As String = "http://MyServer/sites/MySiteCollection"

            Dim clientContext As New ClientContext(siteUrl)
            Dim oList As List = clientContext.Web.Lists.GetByTitle("Announcements")

            clientContext.Load(oList, Function(list) list.ItemCount)

            clientContext.ExecuteQuery()

            Dim startCount As Integer = oList.ItemCount
            Dim oListItem As ListItem = oList.GetItemById(2)

            oListItem.DeleteObject()

            oList.Update()

            clientContext.Load(oList, Function(list) list.ItemCount)

            clientContext.ExecuteQuery()

            Dim endCount As Integer = oList.ItemCount

            Console.WriteLine("Start: {0}  End: {1}", startCount, endCount)
        End Sub
    End Class
End Namespace
var siteUrl = '/sites/MySiteCollection';

function deleteListItemDisplayCount() {

    this.clientContext = new SP.ClientContext(siteUrl);
    this.oList = clientContext.get_web().get_lists().getByTitle('Announcements');

    clientContext.load(oList);

    clientContext.executeQueryAsync(Function.createDelegate(this, this.deleteItem), Function.createDelegate(this, this.onQueryFailed));
}

function deleteItem() {

    this.itemId = 58;
    this.startCount = oList.get_itemCount();
    this.oListItem = oList.getItemById(itemId);

    oListItem.deleteObject();

    oList.update();

    clientContext.load(oList);
        
    clientContext.executeQueryAsync(Function.createDelegate(this, this.displayCount), Function.createDelegate(this, this.onQueryFailed));
}

function displayCount() {

    var endCount = oList.get_itemCount();
    var listItemInfo = 'Item deleted: ' + itemId + 
        '\nStart Count: ' +  startCount + ' End Count: ' + endCount;
        
    alert(listItemInfo)
}

function onQueryFailed(sender, args) {

    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

関連項目

概念

クライアント オブジェクトの作成

クライアント オブジェクト モデルのガイドライン

データ取得の概要

[方法] リスト アイテムを取得する

一般的なプログラミング作業

その他の技術情報

コード スニペット: クライアントで外部リストのアイテムを作成する

コード スニペット: クライアントで外部リストのアイテムを更新する

コード スニペット: クライアントの外部リストからアイテムを削除する

クライアント クラス ライブラリ

ECMAScript クラス ライブラリ