【问题标题】:How can I create new site (not subsite ) in sharepoint online using CSOM如何使用 CSOM 在 sharepoint 中在线创建新站点(不是子站点)
【发布时间】:2018-02-02 06:12:08
【问题描述】:

我想在根级别创建新站点(而不是现有站点中的子站点)。我正在使用 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();

请提出解决方案。

【问题讨论】:

    标签: sharepoint sharepoint-online csom


    【解决方案1】:

    关于要求:

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

    SharePoint terminology 中称为网站集

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

    在 SharePoint CSOM API 中,Tenant class 专门用于此目的,尤其是 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)};
    }
    

    【讨论】:

      猜你喜欢
      • 2020-08-10
      • 1970-01-01
      • 2018-12-11
      • 2013-01-23
      • 2016-11-24
      • 2017-04-29
      • 1970-01-01
      • 1970-01-01
      • 2018-09-02
      相关资源
      最近更新 更多