2017-12-18 202 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)}; 
} 
相關問題