RAHUL
RAHUL

Reputation: 167

Creating site collection programmatically with custom site template in Office 365 APP

I am trying to create a site collection in Office 365 through an auto-hosted app. I am using the Microsoft.Online.SharePoint.Client.Tenant.dll and my code is as below.

using System;
using Microsoft.Online.SharePoint.TenantAdministration;
using Microsoft.SharePoint.Client;
using System.Security;

namespace SharePoint123
{
 class Program
 {
    static void Main(string[] args)
    {
        //please change the value of user name, password, and admin portal URL
        string username = "[email protected]";
        String pwd = "xxxx";
        ClientContext context = new ClientContext("https://xxxx-admin.sharepoint.com");
        SecureString password = new SecureString();
        foreach (char c in pwd.ToCharArray())
        {
            password.AppendChar(c);
        }
        context.Credentials = new SharePointOnlineCredentials(username, password);

        Tenant t = new Tenant(context);
        context.ExecuteQuery();//login into SharePoint online


        //code to create a new site collection
        var newsite = new SiteCreationProperties()
        {
            Url = "https://xxxxx.sharepoint.com/sites/createdbyProgram1",
            Owner = "[email protected]",
            Template = "STS#0", //using the team site template, check the MSDN if you want to use other template
            StorageMaximumLevel = 100,
            UserCodeMaximumLevel = 100,
            UserCodeWarningLevel = 100,
            StorageWarningLevel = 300,
            Title = "CreatedbyPrgram",
            CompatibilityLevel = 15, //15 means Shapoint online 2013, 14 means Sharepoint online 2010

        };
        t.CreateSite(newsite);

        context.ExecuteQuery();
        //end 

    }
  }

}

However, I would like to use a custom site template that I have created, instead of the "STS#0" team site template. The custom site template exists in the Solution Gallery of the site collection in which my APP is deployed. Is there a way I can upload this site template and activate it, during the creation of the new site collection programmatically?

Upvotes: 1

Views: 2958

Answers (1)

Matt
Matt

Reputation: 6050

There's not much documentation on how to use the Microsoft.Online.SharePoint.Client.Tenant.dll with C#, but you can learn it from the SharePoint online Powershell commands because some of the Powershell commands are based on this DLL.

As to create a site collection, the parameter "Template" is the name of the template you want to use, as to find the name of the custom site template, you can use the powershell command "Get-SPOWebTemplate", it will list all the templates with the names.

Note, The Template and LocaleId parameters must be a valid combination as returned from the Get-SPOWebTemplate cmdlet.

Upvotes: 1

Related Questions