简体   繁体   English

SHarePoint 2013网站集通过客户端对象模型

[英]SHarePoint 2013 site collection through client object model

I am working on a requirement where I need to create a site collection in SharePoint using client side api. 我正在制定一项要求,我需要使用客户端api在SharePoint中创建网站集。 I know server side we can do it using self service site creation api. 我知道服务器端我们可以使用自助服务站点创建api。 Also I know in case of SharePoint Online , we have Microsoft.Online.SharePoint.Client.Tenant.dll that we can use to create site collection However in my case I have a On premise environment (SharePoint 2013) where I need to create a site collection thru client side api. 我也知道在SharePoint Online的情况下,我们有Microsoft.Online.SharePoint.Client.Tenant.dll,我们可以用来创建网站集但是在我的情况下,我有一个On premise环境(SharePoint 2013),我需要创建一个网站集通过客户端api。 Can you please let me know if there is any API that I can use for this requirement. 如果有任何API可以用于此要求,请告诉我。

Thanks for any Help you can provide on this. 感谢您提供的任何帮助。

This is not possible to do by using the CSOM, on an on-premise environment. 在内部部署环境中使用CSOM无法做到这一点。

As you mentioned, it is possible on the SPO environment using the library that you listed (Microsoft.Online.SharePoint.Client.Tenant.dll). 如您所述,可以在SPO环境中使用您列出的库(Microsoft.Online.SharePoint.Client.Tenant.dll)。

I'm not sure if this will help, but here is code that could create a site inside of the current site collection: 我不确定这是否会有所帮助,但这里的代码可以在当前网站集中创建一个网站:

You will also need to add using statements for System.Collections.Generic and System.Text.

// Starting with ClientContext, the constructor requires a URL to the 
// server running SharePoint. 
ClientContext context = new ClientContext("http://SiteUrl"); 

WebCreationInformation creation = new WebCreationInformation(); 
creation.Url = "web1"; 
creation.Title = "Hello web1"; 
Web newWeb = context.Web.Webs.Add(creation); 

// Retrieve the new web information. 
context.Load(newWeb, w => w.Title); 
context.ExecuteQuery(); 

label1.Text = newWeb.Title; 

This code was taken directly from here: http://msdn.microsoft.com/en-us/library/fp179912.aspx 此代码直接来自此处: http//msdn.microsoft.com/en-us/library/fp179912.aspx

How to create site collection via SharePoint 2013 Managed CSOM 如何通过SharePoint 2013托管CSOM创建网站集

Tenant.CreateSite method from Microsoft.Online.SharePoint.Client.Tenant.dll assembly is intended for site collection creation: Microsoft.Online.SharePoint.Client.Tenant.dll程序集中的Tenant.CreateSite方法用于创建网站集:

    /// <summary>
    /// Create a new site.
    /// </summary>
    /// <param name="context"></param>
    /// <param name="url">rootsite + "/" + managedPath + "/" + sitename: e.g. "https://auto.contoso.com/sites/site1"</param>
    /// <param name="title">site title: e.g. "Test Site"</param>
    /// <param name="owner">site owner: e.g. admin@contoso.com</param>
    /// <param name="template">The site template used to create this new site</param>
    /// <param name="localeId"></param>
    /// <param name="compatibilityLevel"></param>
    /// <param name="storageQuota"></param>
    /// <param name="resourceQuota"></param>
    /// <param name="timeZoneId"></param>
    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();

    }




//Usage
const string username = "***@***.onmicrosoft.com";
const string password = "***";
const string tenantAdminUrl = "https://***-admin.sharepoint.com/";
const string newSiteCollUrl = "https://contoso.sharepoint.com/sites/finance"
var securedPassword = new SecureString();
foreach (var c in password.ToCharArray()) securedPassword.AppendChar(c);
var credentials = new SharePointOnlineCredentials(username, securedPassword);


using (var context = new ClientContext(tenantAdminUrl))
{
     context.Credentials = credentials;
     CreateSite(context, newSiteCollUrl,username);   

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM