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

时间:2017-12-18 13:09:59

标签: sharepoint sharepoint-online csom

我想在根级别创建新网站(而不是在现有网站中创建子网站)。我正在使用CSOM创建网站。在当前场景中,我需要将站点URL提供给客户端上下文以进行身份​​验证和执行操作。这是伪代码。

Collector.of(
    HashMap::new, 
    (map, e) -> map.computeIfAbsent(e.getMileStone(), k -> new ArrayList<>()).add(e), 
    (left, right) -> {
         right.forEach((key,list) -> left.computeIfAbsent(key, k -> new ArrayList<>()).addAll(list));
         return left;
     })
)

请建议解决方案。

1 个答案:

答案 0 :(得分:0)

关于要求:

  

我想在根级别创建新站点(不是现有的子站点)   位点)。

<{3>}中的

称为网站集

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

在SharePoint CSOM API中SharePoint terminology用于此目的,特别是Tenant class

以下是一个例子:

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