Office 365 – PowerShell script to add / create custom site design to our Tenant

Hi All,

Today, I got an opportunity to write PowerShell script to add our custom site design to the Tenant. So thought to be share with all of us.

I Put detailed comments, tried to be self explanatory.

<#
.SYNOPSIS
Ensures that the Site Design is added to tenant

.PARAMETER CredentialFilePath
 Office 365 system account credential file path having two lines in following format
 UserName
 Password

 .PARAMETER TenantAdminURL
 Office 365 tenant admin site URL

 .PARAMETER SitedesignScriptJSONFilePath
 Path of SitedesignScriptJSONFilePath.json file

  .PARAMETER SiteDesignTitle
 Name of Site Design
 
 .PARAMETER IsDefault
 IS this site design default
#>

    param
    (
    	[parameter(Mandatory=$true)][string]$CredentialFilePath,
        [parameter(Mandatory=$true)][string]$TenantAdminURL,
        [parameter(Mandatory=$true)][string]$SiteDesignJSONFilePath,
        [parameter(Mandatory=$true)][string]$SiteDesignTitle,
        [parameter(Mandatory=$true)][boolean]$IsDefault
    )

    #Check and add PowerShell snap in
    if(-not(Get-PSSnapin | Where { $_.Name -eq "Microsoft.SharePoint.PowerShell"}))
    {
	Add-PSSnapin Microsoft.SharePoint.PowerShell;
    }

    #Get the user credential file path and getting user from it
    $user = Get-Content $CredentialFilePath | Select-Object -First 1
    $password = Get-Content $CredentialFilePath | Select-Object -First 1 -Skip 1
    $securePassword = ConvertTo-SecureString $Password -AsPlainText -Force 

    #Connect to Office365
    $spoManagementCred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $user, $securePassword
    Connect-SPOService -Url $TenantAdminURL -Credential $spoManagementCred

    #Add site script
    $designID = Get-Content $SiteDesignJSONFilePath -Raw | Add-SPOSiteScript -Title "Site Design Script"

	#By default adding for Modern Team sites. We can parameterised this if same we need to apply for modern communocation sites
	#Modern Team Site Template Id - 64
	#Modern Communication Site Template Id - 68
    if($IsDefault)
    {
        Add-SPOSiteDesign -Title $SiteDesignTitle -WebTemplate "64" -SiteScripts $designID -IsDefault -Description "Custom site design demo."
    }
    else
    {
        Add-SPOSiteDesign -Title $SiteDesignTitle -WebTemplate "64" -SiteScripts $designID -Description "Custom site design demo."
    }

    write-host "Design added successfully and set to Default."

References:
Create and use custom SharePoint site designs in Office 365
SharePoint site design and site script overview

Thanks for Reading 🙂

Keep reading, share your thoughts, experiences. Feel free to contact us to discuss more. If you have any suggestion / feedback / doubt, you are most welcome.

Stay tuned on Knowledge-Junction, will come up with more such articles.

This site uses Akismet to reduce spam. Learn how your comment data is processed.