Create SPWeb in Office 365, using Custom Site Template (PowerShell)

When you need to create a LOT of SharePoint subsites, you can do a script with PowerShell – using the Microsoft.SharePoint.Client.WebCreationInformation class/object.

The steps are basically :

  • Connect to O365
  • Define the settings for the new WebSite
  • Add to WEBS
  • Context.ExecuteQuery

Here’s the PowerShell I’m using (see below to copy+paste) :

image

The tricky part – for a CUSTOM WEB TEMPLATE is knowing what “ID” to use.

Ordinarily, the STS#0 will create a team site – but how to get the ID for my custom site template ??

There’s a quick tip – via the UI – when you go to create a new SUB SITE – using the F12 developer tools.    See that <option> tag ??

<option value=”{24ECC832-383F-4C5E-B658-9A640EA73509}#Client Site Template”>Client Site Template</option>

image

*THAT* is the value you need to include – in the PowerShell as above…

And then you just call the “CreateSubSite” function – and stamp ‘em out.

Smile

======================================

$urlAdmin = “https://AVENGERS-admin.sharepoint.com

$user = “TONY.STARK@AVENGERS.com”

$password = “PepperPottsIsAwesome!”

$urlSite = “https://AVENGERS.sharepoint.com/sites/clients”

$passwordSecureString = ConvertTo-SecureString -string $password -AsPlainText -Force

$credential = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $user, $passwordSecureString

Connect-SPOService -Url $urlAdmin -Credential $credential

$spoCredentials = New-Object -TypeName Microsoft.SharePoint.Client.SharePointOnlineCredentials -argumentlist $user, $passwordSecureString

$spoCtx = New-Object Microsoft.SharePoint.Client.ClientContext($urlSite)

$spoCtx.Credentials = $spoCredentials

$spoCtx.RequestTimeout = “500000”

function CreateSubSite($title)

{

$WCI = New-Object Microsoft.SharePoint.Client.WebCreationInformation

$WCI.WebTemplate = “{24ECC832-383F-4C5E-B658-9A640EA73509}#Client Site Template”

$WCI.Description = $title

$WCI.Title = $title

$WCI.Url = $title

$WCI.Language = “1033”

$spoCtx.Web.Webs.Add($WCI)

$spoCtx.ExecuteQuery()

}

CreateSubSite “Ragnarok”

CreateSubSite “Anotherrok”

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s