如何:创建、更新和删除列表项

上次修改时间: 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)) 时生效。下面的示例设置"通知"列表中的第三个项的标题。

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)) 从列表返回第二个项,然后删除该项。

SharePoint Foundation 2010 将保留集合中项的整数 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),然后再执行查询。若要检索列表项的开始计数和结束计数,则必须执行两次查询并返回项计数两次,如上一示例的以下修改内容所示。

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 类库