如何:使用网站

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

适用范围: SharePoint Foundation 2010

本文内容
检索网站的属性
仅检索网站的指定属性
更新网站的标题和说明
创建网站

在 SharePoint Online 中提供

若要使用网站,最初应使用 ClientContext() 构造函数 (JavaScript: ClientContext(serverRelativeUrl)) 并传递 URL 或 URI 以返回一个特定的请求上下文。

检索网站的属性

使用 ClientContext 类 (JavaScripthttps://msdn.microsoft.com/zh-cn/library/ff408569(v=office.14)) 的 Web 属性 (JavaScript: web) 来指定位于指定的上下文 URL 处的网站对象的属性。在通过 Load<T>(T, []) 方法 (JavaScript: load(clientObject)) 下载网站对象之后,调用 ExecuteQuery()ExecuteQueryAsync(ClientRequestSucceededEventHandler, ClientRequestFailedEventHandler) (JavaScript: executeQueryAsync(succeededCallback, failedCallback)),可获取对网站的所有属性的访问权。下面的示例显示了指定网站集中的根网站的标题和说明,但在您下载网站对象并执行查询之后,默认返回的所有其他属性均会变为可用的。有关在检索特定的客户端对象时默认不可用的属性列表的详细信息,请参见数据检索概述

using System;
using Microsoft.SharePoint.Client;

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

            ClientContext clientContext = new ClientContext(siteUrl);

            Web oWebsite = clientContext.Web;

            clientContext.Load(oWebsite);

            clientContext.ExecuteQuery();

            Console.WriteLine("Title: {0} Description: {1}", oWebsite.Title, oWebsite.Description);
        }
    }
}
Imports System
Imports Microsoft.SharePoint.Client

Namespace Microsoft.SDK.SharePointServices.Samples
    Class RetrieveWebsite

        Shared Sub Main()

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

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

            clientContext.Load(oWebsite)

            clientContext.ExecuteQuery()

            Console.WriteLine("Title: {0} Description: {1}", oWebsite.Title, oWebsite.Description)
        End Sub
    End Class
End Namespace
var siteUrl = '/sites/MySiteCollection ';
    
function retrieveWebSite() {
    var clientContext = new SP.ClientContext(siteUrl);
    this.oWebsite = clientContext.get_web();

    clientContext.load(this.oWebsite);

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

function onQuerySucceeded(sender, args) {
    alert('Title: ' + this.oWebsite.get_title() + ' Decription: ' + this.oWebsite.get_description());
}
    
function onQueryFailed(sender, args) {
    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

仅检索网站的指定属性

若要减少客户端和服务器之间不必要的数据传输,可能需要仅返回网站对象的指定属性,而不是返回网站对象的所有属性。在这种情况下,可将 LINQ 查询或 lambda 表达式语法与 Load<T>(T, []) (JavaScript: load(clientObject)) 方法一起使用,以指定从服务器返回哪些属性。在下面的示例中,在调用 ExecuteQuery() (JavaScript: executeQueryAsync(succeededCallback, failedCallback)) 之后,只有网站对象的标题和创建日期是可用的。例如,如果您尝试将 oWebsite.Description 写入到控制台,则会接收 PropertyOrFieldNotInitializedException

using System;
using Microsoft.SharePoint.Client;

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

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

            clientContext.Load(
                oWebsite,
                website => website.Title,
                website => website.Created);

            clientContext.ExecuteQuery();

            Console.WriteLine("Title: {0} Created: {1}", oWebsite.Title, oWebsite.Created);
        }
    }
}
Imports System
Imports Microsoft.SharePoint.Client

Namespace Microsoft.SDK.SharePointServices.Samples
    Class RetrieveWebsiteProperties

        Shared Sub Main()

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

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

            clientContext.Load(oWebsite, _ 
                           Function(website) website.Title, _ 
                           Function(website) website.Created)

            clientContext.ExecuteQuery()

            Console.WriteLine("Title: {0} Created: {1}", oWebsite.Title, oWebsite.Created)
        End Sub
    End Class
End Namespace
var siteUrl = '/sites/MySiteCollection ';
    
function retrieveWebSiteProperties() {
    var clientContext = new SP.ClientContext(siteUrl);
    this.oWebsite = clientContext.get_web();

    clientContext.load(this.oWebsite, 'Title', 'Created');

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

function onQuerySucceeded(sender, args) {
    alert('Title: ' + this.oWebsite.get_title() + ' Created: ' + this.oWebsite.get_created());
}
    
function onQueryFailed(sender, args) {
    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

请注意,在 JavaScript 中,ClientContext(serverRelativeUrl) 构造函数采用相对于服务器的 URL,load(clientObject) 方法接受字符串而不是实际的 LINQ 对象。

更新网站的标题和说明

若要修改网站,可设置网站的属性并调用 Update() (JavaScript: update()) 方法,这与服务器对象模型的工作方式相似。但在客户端对象模型中,必须调用 ExecuteQuery()ExecuteQueryAsync(ClientRequestSucceededEventHandler, ClientRequestFailedEventHandler) (JavaScript: executeQuery(succeededCallback, failedCallback)) 来请求对您指定的所有命令进行批处理。下面的示例更改指定网站的标题和说明。

using System;
using Microsoft.SharePoint.Client;

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

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

            oWebsite.Title = "Updated Web Site";
            oWebsite.Description = "This is an updated Web site.";

            oWebsite.Update();

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

Namespace Microsoft.SDK.SharePointServices.Samples
    Class UpdateWebSite

        Shared Sub Main()

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

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

            oWebsite.Title = "Updated Web Site"
            oWebsite.Description = "This is an updated Web site."

            oWebsite.Update()

            clientContext.ExecuteQuery()
        End Sub
     End Class
End Namespace
var siteUrl = '/sites/MySiteCollection ';
    
function updateWebSite() {
    var clientContext = new SP.ClientContext(siteUrl);
    this.oWebsite = clientContext.get_web();

    this.oWebsite.set_title('Updated Web Site');
    this.oWebsite.set_description('This is an updated Web site.');
    this.oWebsite.update();

    clientContext.load(this.oWebsite, 'Title', 'Description');

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

function onQuerySucceeded(sender, args) {
    alert('Title: ' + this.oWebsite.get_title() + ' Decription: ' + this.oWebsite.get_description());
}
    
function onQueryFailed(sender, args) {
    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

创建网站

若要创建网站对象,请使用 WebCreationInformation (JavaScript: WebCreationInformation) 类来定义其属性,再将此对象传递到 WebCollectionJavaScripthttps://msdn.microsoft.com/zh-cn/library/ff408097(v=office.14) 类的 Add(WebCreationInformation) (JavaScript: add(parameters)) 方法。

下面的示例在一个网站集内创建新的博客网站。

using System;
using Microsoft.SharePoint.Client;

namespace Microsoft.SDK.SharePointServices.Samples
{
    class CreateWebSite
    {
        static void Main()
        {
            string siteUrl = "http://MyServer/sites/MySiteCollection";
            string blogDescription = "A new blog Web site.";
            int blogLanguage = 1033;
            string blogTitle = "Blog Web Site";
            string blogUrl = "blogwebsite";
            bool blogPermissions = false;
            string webTemplate = "BLOG#0";

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

            WebCreationInformation webCreateInfo = new WebCreationInformation();
            webCreateInfo.Description = blogDescription;
            webCreateInfo.Language = blogLanguage;
            webCreateInfo.Title = blogTitle;
            webCreateInfo.Url = blogUrl;
            webCreateInfo.UseSamePermissionsAsParentSite = blogPermissions;
            webCreateInfo.WebTemplate = webTemplate;

            Web oNewWebsite = oWebsite.Webs.Add(webCreateInfo);

            clientContext.Load(
                oNewWebsite,
                website => website.ServerRelativeUrl,
                website => website.Created);

            clientContext.ExecuteQuery();

            Console.WriteLine("Server-relative Url: {0} Created: {1}", oNewWebsite.ServerRelativeUrl, oNewWebsite.Created);
        }
    }
}
Imports System
Imports Microsoft.SharePoint.Client

Namespace Microsoft.SDK.SharePointServices.Samples
    Class CreateWebSite

        Shared Sub Main()

            Dim siteUrl As String = "http://MyServer/sites/MySiteCollection"
            Dim blogDescription As String = "A new Blog Web site."
            Dim blogLanguage As Integer = 1033
            Dim blogTitle As String = "Blog Web Site"
            Dim blogUrl As String = "blogwebsite"
            Dim blogPermissions As Boolean = False
            Dim webTemplate As String = "BLOG#0"

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

            Dim webCreateInfo As New WebCreationInformation()
            webCreateInfo.Description = blogDescription
            webCreateInfo.Language = blogLanguage
            webCreateInfo.Title = blogTitle
            webCreateInfo.Url = blogUrl
            webCreateInfo.UseSamePermissionsAsParentSite = blogPermissions
            webCreateInfo.WebTemplate = webTemplate

            Dim oNewWebsite As Web = oWebsite.Webs.Add(webCreateInfo)

            clientContext.Load(oWebsite, _ 
                               Function(website) website.ServerRelativeUrl, _ 
                               Function(website) website.Created)

            clientContext.ExecuteQuery()

            Console.WriteLine("Server-relative Url: {0} Created: {1}", oWebsite.ServerRelativeUrl, oWebsite.Created)
        End Sub
    End Class
End Namespace
var siteUrl = '/sites/MySiteCollection ';
    
function createWebSite() {
    var blogDescription = 'A new Blog Web site.';
    var blogLanguage = 1033;
    var blogTitle = 'Blog Web Site';
    var blogUrl = 'blogwebsite';
    var blogPermissions = false;
    var webTemplate = 'BLOG#0';

    var clientContext = new SP.ClientContext(siteUrl);
    this.oWebsite = clientContext.get_web();

    var webCreateInfo = new SP.WebCreationInformation();
    webCreateInfo.set_description(blogDescription);
    webCreateInfo.set_language(blogLanguage);
    webCreateInfo.set_title(blogTitle);
    webCreateInfo.set_url(blogUrl);
    webCreateInfo.set_useSamePermissionsAsParentSite(blogPermissions);
    webCreateInfo.set_webTemplate(webTemplate);

    this.oNewWebsite = this.oWebsite.get_webs().add(webCreateInfo);

    clientContext.load(this.oNewWebsite, 'ServerRelativeUrl', 'Created');

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

function onQuerySucceeded(sender, args) {
    alert('Title: ' + this.oWebsite.get_title() + ' Decription: ' + this.oWebsite.get_description());
}
    
function onQueryFailed(sender, args) {
    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

上一示例使用 LINQ 查询表达式来返回 ServerRelativeUrl (JavaScript: serverRelativeUrl) 和 Created (JavaScript: created) 属性,这些属性的值是不可显示的,除非您明确要求显示。

备注

当使用 LINQ 创建针对客户端对象模型的查询时,使用的是 LINQ to Objects,而不是 LINQ to SharePoint 提供程序,该提供程序只有在编写针对服务器对象模型的代码时才能使用。

有关在 SharePoint Foundation Silverlight 对象模型的上下文内如何与网站一起使用的信息和示例,请参见使用 Silverlight 对象模型

请参阅

概念

数据检索概述

创建客户端对象

客户端对象模型准则

常见编程任务

其他资源

客户端类库

ECMAScript 类库