【问题标题】:SHarePoint 2013 site collection through client object modelSHarePoint 2013 网站集通过客户端对象模型
【发布时间】:2013-08-04 15:31:00
【问题描述】:
我正在处理需要使用客户端 api 在 SharePoint 中创建网站集的要求。我知道服务器端我们可以使用自助服务站点创建 api 来做到这一点。我也知道在 SharePoint Online 的情况下,我们有 Microsoft.Online.SharePoint.Client.Tenant.dll 可以用来创建网站集但是在我的情况下,我有一个本地环境(SharePoint 2013),我需要在其中创建一个通过客户端 api 收集网站。如果有任何 API 可用于此要求,请告诉我。
感谢您对此提供的任何帮助。
【问题讨论】:
标签:
sharepoint
sharepoint-2013
【解决方案1】:
在本地环境中使用 CSOM 无法做到这一点。
正如您所提到的,可以在 SPO 环境中使用您列出的库 (Microsoft.Online.SharePoint.Client.Tenant.dll)。
我不确定这是否会有所帮助,但这里的代码可以在当前网站集中创建一个网站:
You will also need to add using statements for System.Collections.Generic and System.Text.
// Starting with ClientContext, the constructor requires a URL to the
// server running SharePoint.
ClientContext context = new ClientContext("http://SiteUrl");
WebCreationInformation creation = new WebCreationInformation();
creation.Url = "web1";
creation.Title = "Hello web1";
Web newWeb = context.Web.Webs.Add(creation);
// Retrieve the new web information.
context.Load(newWeb, w => w.Title);
context.ExecuteQuery();
label1.Text = newWeb.Title;
此代码直接取自这里:http://msdn.microsoft.com/en-us/library/fp179912.aspx
【解决方案2】:
如何通过 SharePoint 2013 托管 CSOM 创建网站集
Tenant.CreateSite method 来自 Microsoft.Online.SharePoint.Client.Tenant.dll 程序集用于创建网站集:
/// <summary>
/// Create a new site.
/// </summary>
/// <param name="context"></param>
/// <param name="url">rootsite + "/" + managedPath + "/" + sitename: e.g. "https://auto.contoso.com/sites/site1"</param>
/// <param name="title">site title: e.g. "Test Site"</param>
/// <param name="owner">site owner: e.g. admin@contoso.com</param>
/// <param name="template">The site template used to create this new site</param>
/// <param name="localeId"></param>
/// <param name="compatibilityLevel"></param>
/// <param name="storageQuota"></param>
/// <param name="resourceQuota"></param>
/// <param name="timeZoneId"></param>
internal static void CreateSite(ClientContext context, String url, String owner, String title =null, String template = null, uint? localeId = null, int? compatibilityLevel = null, long? storageQuota = null, double? resourceQuota = null, int? timeZoneId = null)
{
var tenant = new Tenant(context);
if (url == null)
throw new ArgumentException("Site Url must be specified");
if (string.IsNullOrEmpty(owner))
throw new ArgumentException("Site Owner must be specified");
var siteCreationProperties = new SiteCreationProperties {Url = url, Owner = owner};
if (!string.IsNullOrEmpty(template))
siteCreationProperties.Template = template;
if (!string.IsNullOrEmpty(title))
siteCreationProperties.Title = title;
if (localeId.HasValue)
siteCreationProperties.Lcid = localeId.Value;
if (compatibilityLevel.HasValue)
siteCreationProperties.CompatibilityLevel = compatibilityLevel.Value;
if (storageQuota.HasValue)
siteCreationProperties.StorageMaximumLevel = storageQuota.Value;
if (resourceQuota.HasValue)
siteCreationProperties.UserCodeMaximumLevel = resourceQuota.Value;
if (timeZoneId.HasValue)
siteCreationProperties.TimeZoneId = timeZoneId.Value;
var siteOp = tenant.CreateSite(siteCreationProperties);
context.Load(siteOp);
context.ExecuteQuery();
}
//Usage
const string username = "***@***.onmicrosoft.com";
const string password = "***";
const string tenantAdminUrl = "https://***-admin.sharepoint.com/";
const string newSiteCollUrl = "https://contoso.sharepoint.com/sites/finance"
var securedPassword = new SecureString();
foreach (var c in password.ToCharArray()) securedPassword.AppendChar(c);
var credentials = new SharePointOnlineCredentials(username, securedPassword);
using (var context = new ClientContext(tenantAdminUrl))
{
context.Credentials = credentials;
CreateSite(context, newSiteCollUrl,username);
}