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

上次修改时间: 2011年4月29日

适用范围: SharePoint Foundation 2010

本文内容
创建和更新列表
向列表中添加字段
删除列表

在 SharePoint Online 中提供

通过客户端对象模型创建、更新和删除列表与使用服务器对象模型执行这些任务的方式类似,只是在调用 ExecuteQuery()ExecuteQueryAsync(ClientRequestSucceededEventHandler, ClientRequestFailedEventHandler) 方法 (JavaScript: executeQueryAsync(succeededCallback, failedCallback)) 之前,客户端操作不会完成。

创建和更新列表

若要创建列表对象,请使用 ListCreationInformation 类 (JavaScript: ListCreationInformation) 来定义其属性,然后将此对象传递给 ListCollection 类 (JavaScript: ListCollection) 的 Add(ListCreationInformation) 方法 (JavaScript: add(parameters))。下面的示例创建了一个新的通知列表。

using System;
using Microsoft.SharePoint.Client;

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

            ClientContext clientContext = new ClientContext(siteUrl);
            Web oWebsite = clientContext.Web;

            ListCreationInformation listCreationInfo = new ListCreationInformation();
            listCreationInfo.Title = "My Announcements List";
            listCreationInfo.TemplateType = (int)ListTemplateType.Announcements;

            List oList = oWebsite.Lists.Add(listCreationInfo);

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

Namespace Microsoft.SDK.SharePointServices.Samples
    Class CreateList

        Shared Sub Main()

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

            Dim clientContext As New ClientContext(siteUrl)
            Dim oWebsite As Web = clientContext.Web

            Dim listCreationInfo As New ListCreationInformation()
            listCreationInfo.Title = "My Announcements List"
            listCreationInfo.TemplateType = CInt(ListTemplateType.Announcements)

            Dim oList As List = oWebsite.Lists.Add(listCreationInfo)

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

function createList() {
    var clientContext = new SP.ClientContext(siteUrl);
    var oWebsite = clientContext.get_web();
    
    var listCreationInfo = new SP.ListCreationInformation();
    listCreationInfo.set_title('My Announcements List');
    listCreationInfo.set_templateType(SP.ListTemplateType.announcements);

    this.oList = oWebsite.get_lists().add(listCreationInfo);

    clientContext.load(oList);

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

function onQuerySucceeded() {
    var result = oList.get_title() + ' created.';
    alert(result);
}

function onQueryFailed(sender, args) {
    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

如果添加列表后需要更新列表,可以在调用 ExecuteQuery()ExecuteQueryAsync(ClientRequestSucceededEventHandler, ClientRequestFailedEventHandler) (JavaScript: executeQueryAsync(succeededCallback, failedCallback)) 之前设置列表属性并调用 Update() 方法 (JavaScript: update()),请参考下面对前面的示例所做的修改。

            .
            .
            .
            .
            List oList = oWebsite.Lists.Add(listCreationInfo);

            oList.Description = "New Announcements List";

            oList.Update();

            clientContext.ExecuteQuery();
            .
            .
            .
            .
            Dim oList As List = oWebsite.Lists.Add(listCreationInfo)

            oList.Description = "New Announcements List"

            oList.Update()

            clientContext.ExecuteQuery()
            .
            .
            .
            .
            this.oList = oWebsite.get_lists().add(listCreationInfo);

            oList.set_description('New Announcements List');

            oList.update();

            clientContext.load(oList);

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

向列表中添加字段

使用 FieldCollection 类 (JavaScript: FieldCollection) 的 Add(Field) 方法 (JavaScript: add(field)) 或 AddFieldAsXml(String, Boolean, AddFieldOptions) 方法 (JavaScript: addFieldAsXml(schemaXml, addToDefaultView, options)) 可向列表的字段集合中添加字段。下面的示例创建了一个字段,并在调用 ExecuteQuery() (JavaScript: executeQueryAsync(succeededCallback, failedCallback)) 之前更新了该字段。

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

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

            ClientContext clientContext = new ClientContext(siteUrl);

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

            SP.Field oField = oList.Fields.AddFieldAsXml("<Field DisplayName='MyField' Type='Number' />",

                true, AddFieldOptions.DefaultValue);

            SP.FieldNumber fieldNumber = clientContext.CastTo<FieldNumber>(oField);
            fieldNumber.MaximumValue = 100;
            fieldNumber.MinimumValue = 35;

            fieldNumber.Update();

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

Namespace Microsoft.SDK.SharePointServices.Samples
    Class AddFieldToList

        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("My Announcements List")

            Dim oField As SP.Field = oList.Fields.AddFieldAsXml( _
                "<Field DisplayName='MyField' Type='Number' />", _
                True, AddFieldOptions.DefaultValue)

            Dim fieldNumber As SP.FieldNumber = clientContext.CastTo(Of FieldNumber)(oField)

            fieldNumber.MaximumValue = 100
            fieldNumber.MinimumValue = 35

            fieldNumber.Update()

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

function addFieldToList() {
    var clientContext = new SP.ClientContext(siteUrl);

    var oList = clientContext.get_web().get_lists().getByTitle('Announcements');

    this.oField = oList.get_fields().addFieldAsXml('<Field DisplayName=\'MyField\' Type=\'Number\' />', true, SP.AddFieldOptions.defaultValue);

    var fieldNumber = clientContext.castTo(oField,SP.FieldNumber);
    fieldNumber.set_maximumValue(100);
    fieldNumber.set_minimumValue(35);

    fieldNumber.update();

    clientContext.load(oField);

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

function onQuerySucceeded() {
    var result = oField.get_title() + ' added.';
    alert(result);
}

function onQueryFailed(sender, args) {
    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

前面的示例使用客户端上下文对象的 CastTo<T>(ClientObject) 方法 (JavaScript: castTo(obj, type)) 将字段作为 FieldNumber 类型 (JavaScript: FieldNumber) 强制转换,必须在执行查询之前完成此操作。否则,客户端对象模型将无法知道返回对象 oField 的实际类型,默认情况下,将使用 Field (JavaScript: Field) 作为类型。

删除列表

若要删除列表,请调用列表对象的 DeleteObject() 方法 (JavaScript: deleteObject()),如下面的示例所示。

using System;
using Microsoft.SharePoint.Client;

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

            ClientContext clientContext = new ClientContext(siteUrl);
            Web oWebsite = clientContext.Web;

            List oList = oWebsite.Lists.GetByTitle("My Announcements List");

            oList.DeleteObject();

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

Namespace Microsoft.SDK.SharePointServices.Samples
    Class DeleteList

        Shared Sub Main()

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

            Dim clientContext As New ClientContext(siteUrl)
            Dim oWebsite As Web = clientContext.Web

            Dim oList As List = oWebsite.Lists.GetByTitle("My Announcements List")

            oList.DeleteObject()

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

function deleteList() {
    var clientContext = new SP.ClientContext(siteUrl);
    var oWebsite = clientContext.get_web();
    this.listTitle = 'My Announcements List';

    this.oList = oWebsite.get_lists().getByTitle(listTitle);

    oList.deleteObject();

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

function onQuerySucceeded() {
    var result = listTitle + ' deleted.';
    alert(result);
}

function onQueryFailed(sender, args) {
    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

有关在 Microsoft SharePoint Foundation 2010 Silverlight 对象模型的上下文中使用列表和其他客户端对象的信息和示例,请参阅使用 Silverlight 对象模型

请参阅

概念

创建客户端对象

客户端对象模型准则

数据检索概述

如何:检索列表

常见编程任务

其他资源

客户端类库

ECMAScript 类库