如何使用 CSOM 在 Sharepoint online 中创建新站点(不是子站点)

How can I create new site (not subsite ) in sharepoint online using CSOM

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

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

请提出解决方案。

关于要求:

I want to create new site at root level (not subsite in existing site).

SharePoint terminology 中称为 网站集:

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

在 SharePoint CSOM API Tenant class is intended for that purpose, in particular Tenant.CreateSite method 中。

这是一个例子:

const string username = "username@contoso.onmicrosoft.com";
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)};
}