创建客户端对象

上次修改时间: 2011年5月27日

适用范围: SharePoint Foundation 2010

在 SharePoint Online 中提供

如果可以创建客户端对象(如 List 对象 (JavaScript: List)),那么其相应的客户端对象集合(如 ListCollection (JavaScript: ListCollection))将具有一个接受 ClientObjectCreationInformation 对象(如 ListCreationInformation (JavaScript: ListCreationInformation))的 Add 方法,该方法包含创建新对象所需的全部信息。

以下示例结合使用 WebCreationInformation (JavaScript: WebCreationInformation) 和 Add(WebCreationInformation) 方法 (JavaScript: add(parameters)) 在指定网站下创建一个子网站。

ClientContext clientContext = new ClientContext("http://MyServer/sites/MySiteCollection/MyWebSite");
WebCollection collWeb = clientContext.Web.Webs;

WebCreationInformation webCreationInfo = new WebCreationInformation();
webCreationInfo.Title = "My New Web Site";
webCreationInfo.Description = "Description of new Web site...";
webCreationInfo.Language = 1033;
webCreationInfo.Url = "MyNewWebSite";
webCreationInfo.UseSamePermissionsAsParentSite = true;
webCreationInfo.WebTemplate = "STS#0";

Web oNewWebsite = collWeb.Add(webCreationInfo);
clientContext.ExecuteQuery();
Dim clientContext As New ClientContext("http://MyServer/sites/MySiteCollection/MyWebSite")
Dim collWeb As WebCollection = clientContext.Web.Webs
         
Dim webCreationInfo As New WebCreationInformation()
webCreationInfo.Title = "My New Web Site"
webCreationInfo.Description = "Description of new Web site..."
webCreationInfo.Language = 1033
webCreationInfo.Url = "MyNewWebSite"
webCreationInfo.UseSamePermissionsAsParentSite = True
webCreationInfo.WebTemplate = "STS#0"
         
Dim oNewWebsite As Web = collWeb.Add(webCreationInfo)
clientContext.ExecuteQuery()
function createWebsite() {

    var clientContext = new SP.ClientContext('http://MyServer/sites/MySiteCollection/MyWebSite');
    var collWeb = clientContext.get_web().get_webs();

    var webCreationInfo = new SP.WebCreationInformation();
    webCreationInfo.set_title('My New Web Site');
    webCreationInfo.set_description('Description of new Web site...');
    webCreationInfo.set_language(1033);
    webCreationInfo.set_url('MyNewWebSite');
    webCreationInfo.set_useSamePermissionsAsParentSite(true);
    webCreationInfo.set_webTemplate('STS#0');

    var oNewWebsite = collWeb.add(webCreationInfo);


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

function onQuerySucceeded() {
    alert("Created Web site.");
}

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

在上面的示例中,Add(WebCreationInformation) 方法 (JavaScript: add(parameters)) 立即在客户端对象集合中创建一个网站,而不等待进行后续集合数据检索或调用 ExecuteQuery()ExecuteQueryAsync(ClientRequestSucceededEventHandler, ClientRequestFailedEventHandler) (JavaScript: executeQueryAsync(succeededCallback, failedCallback))。

您可以使用 ClientObjectCreationInformation 对象创建许多对象。例如,创建导航节点也涉及到使用 ClientObjectCreationInformation 对象。以下示例通过 NavigationNodeCreationInformation 对象 (JavaScript: NavigationNodeCreationInformation) 定义导航节点,并使用该对象在指定网站的"快速启动"区域添加一个链接。该示例添加一个指向其他网站上列表的链接作为顶级"列表"节点下面的第二个子节点。

ClientContext clientContext = new ClientContext("http://MyServer/sites/MySiteCollection/MyWebSite");

NavigationNodeCollection collNavNode = clientContext.Web.Navigation.QuickLaunch;

clientContext.Load(collNavNode);
clientContext.ExecuteQuery();

NavigationNodeCollection collChildNavNode = collNavNode[1].Children;

clientContext.Load(collChildNavNode);
clientContext.ExecuteQuery();

NavigationNodeCreationInformation navNodeCreationInfo = new NavigationNodeCreationInformation();

navNodeCreationInfo.PreviousNode = collChildNavNode[0];
navNodeCreationInfo.Title = "My Navigation Node";
navNodeCreationInfo.Url = "http://MyServer/sites/MySiteCollection/MyTargetListWebSite/Lists/MyTargetList/AllItems.aspx";

collChildNavNode.Add(navNodeCreationInfo);

clientContext.ExecuteQuery();
Dim clientContext As New ClientContext("http://MyServer/sites/MySiteCollection/MyWebSite")
         
Dim collNavNode As NavigationNodeCollection = clientContext.Web.Navigation.QuickLaunch
         
clientContext.Load(collNavNode)
clientContext.ExecuteQuery()
         
Dim collChildNavNode As NavigationNodeCollection = collNavNode(1).Children
         
clientContext.Load(collChildNavNode)
clientContext.ExecuteQuery()
         
Dim navNodeCreationInfo As New NavigationNodeCreationInformation()
         
navNodeCreationInfo.PreviousNode = collChildNavNode(0)
navNodeCreationInfo.Title = "My Navigation Node"
navNodeCreationInfo.Url = "http://MyServer/sites/MySiteCollection/MyTargetListWebSite/Lists/MyTargetList/AllItems.aspx"
         
collChildNavNode.Add(navNodeCreationInfo)
         
clientContext.ExecuteQuery()
function getNavNodes() {
    this.clientContext = new SP.ClientContext('/sites/MySiteCollection/MyWebSite');
    this.collNavNode = clientContext.get_web().get_navigation().get_quickLaunch();

    clientContext.load(collNavNode);
    clientContext.executeQueryAsync(Function.createDelegate(this, this.getChildNavNodes), Function.createDelegate(this, this.onQueryFailed));
}

function getChildNavNodes() {

    this.collChildNavNode = collNavNode.get_item(1).get_children();
    clientContext.load(collChildNavNode);
    clientContext.executeQueryAsync(Function.createDelegate(this, this.createNavNode), Function.createDelegate(this, this.onQueryFailed));
}

function createNavNode() {

    var navNodeCreationInfo = new SP.NavigationNodeCreationInformation();
    navNodeCreationInfo.set_previousNode(collChildNavNode.get_item(0));
    navNodeCreationInfo.set_title('My Navigation ECMA Node');
    navNodeCreationInfo.set_url('http://MyServer/sites/MySiteCollection/MyTargetListWebSite/Lists/MyTargetList/AllItems.aspx');

    collChildNavNode.add(navNodeCreationInfo);

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

function onQuerySucceeded() {

    alert("Created navigation node.");
}

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

上面的示例创建一个指向网站集中其他网站的链接。若要创建一个指向网站集外部位置的外部链接,请将 NavigationNodeCreationInformation 对象 (JavaScript: NavigationNodeCreationInformation) 的 IsExternal 属性 (JavaScript: IsExternal) 设置为 true,然后再调用 Add(NavigationNodeCreationInformation) 方法 (JavaScript: add(parameters))。

有关在 Silverlight 应用程序上下文中创建客户端对象的信息,请参阅使用 Silverlight 对象模型

请参阅

概念

如何:使用网站

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

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

如何:使用用户和组

对象模型层次结构和标识

作为中心对象的客户端上下文

客户端对象、值对象和标量属性

数据检索概述

客户端对象模型准则

托管对象模型和 ECMAScript 对象模型的区别

常见编程任务

其他资源

客户端类库

ECMAScript 类库

使用 SharePoint Foundation 2010 托管客户端对象模型

客户端对象模型资源中心(该链接可能指向英文页面)