2017-12-18 190 views
0

我想在根级别(而不是现有站点中的子站点)创建新站点。我正在使用CSOM创建网站。在当前的情况下,我需要提供站点URL到客户端上下文进行身份验证并执行操作。这是一个伪代码。如何在使用CSOM的在线SharePoint Share中创建新站点(不是子站点)

string url = "https://mysharepoint.com/sites/testsite"; 

        SecureString f_SecurePass = new SecureString(); 
        foreach (char ch in pass) 
         f_SecurePass.AppendChar(ch); 

        clientcontext = new ClientContext(url); 

        var credentials = new SharePointOnlineCredentials(userid, f_SecurePass);     

        clientcontext.Credentials = credentials; 
        Web web = clientcontext.Web; 

        clientcontext.Load(web, website => website.Lists); 
        clientcontext.ExecuteQuery(); 

       WebCreationInformation wci = new WebCreationInformation(); 
       wci.Url = "/TestAPISite2"; 
       wci.Title = "TestAPISite2"; 
       wci.Language = 1033; 

       var newsite = clientcontext.Site.RootWeb.Webs.Add(wci); 
       clientcontext.ExecuteQuery(); 

请建议解决方案。

回答

0

关于要求:

我要创建在根级别的新网站(而不是在现有的 网站子站点)。

SharePoint terminology它被称为网站集

  • site collection/top level site/parent site - 网站集 是一个网站,可以包含子网站(又名网)

在SharePoint CSOM API Tenant class专门用于此目的,特别是Tenant.CreateSite method

下面是一个例子:

const string username = "[email protected]"; 
const string password = "password"; 
const string tenantAdminUrl = "https://contoso-admin.sharepoint.com/"; 

using (var ctx = GetContext(tenantAdminUrl,userName,password)) 
{ 
    CreateSite(ctx, "https://contoso.sharepoint.com/sites/marketing","Marketing"); 
} 

其中

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(); 
} 

public static ClientContext GetContext(string url, string userName, string password) 
{ 
    var securePassword = new SecureString(); 
    foreach (var ch in password) securePassword.AppendChar(ch); 
    return new ClientContext(url) {Credentials = new SharePointOnlineCredentials(userName, securePassword)}; 
} 
相关问题