Office 365 – PowerShell Script – Add custom site design to the O365 tenant using PnP on behalf of Azure App (using Azure App credentials)

Hi All,

In one of my previous article “Office 365 – PowerShell script to add / create custom site design to our Tenant” we discussed regarding adding site design to O365 tenant using PowerShell script.

For more details about Site Designs and Site scripts please refer SharePoint site design and site script overview

In that article we connected O365 tenant using user credential where user has “Global Admin” rights. Now we have case like we don’t have user who have “Global Admin” rights. But we have azure app created which having rights on whole tenant.

Please refer my two following articles for creating an azure app and giving rights to tenant. 

Office 365 – Azure Active Directory – Registering/Creating new Azure App – detailed steps

Office 365 – Microsoft Graph – Part 2 – Granting permission to Azure Apps to use Microsoft Graph APIs using CSOM Here, we now we will discuss how connect O365 using azure appid and client secret key and add new site design to O365 tenant. We will use PnP PowerShell cmdlets.

Following are the detailed steps:

  • Connect to O365 / SharePoint Online – using Azure AppId and App client secret key. Here please notice that we are not using any user.
Connect-PnPOnline -AppId "<Azure AppId>" -Url "<SharePoint admin URL>" -AppSecret "<Azure App client secret key>"
  • Once we connected O365, we are ready to add custom site design. For adding site design, we need site script. [Site scripts are JSON files which specify an ordered list of actions which are executed when we create new site.] We will get script content by using Get-Content script
$content = Get-Content "<Path of JSON file>" -Raw
  • Once we have site script content, we are ready to add script  
Add-PnPSiteScript -Title "<Script Title>" -Content $content 

This command will return Site Script ID as, this ID can be used while adding site design / executing cmdlet of site design

Id                                                                       Title                 Version Content

45116cea-2abb-4cf9-98da-ecd131cdf55c <Script Title> 0

  • Once we have site script ID, we are ready to add site design as
Add-PnPSiteDesign -Title "<Title of my custom site design>" -SiteScriptIds "45116cea-2abb-4cf9-98da-ecd131cdf55c” -WebTemplate 68  

Here, web template 68 is for communication site. (Just for reference – Web Template for Modern Team site with group is 64 and web template for Modern Team site without group is 1.)  Here, site design is based on communication site. If command executed successfully we will get Site Design ID as

Id                                                                         Title                                 Site Scripts 5fde7e54-0e84-4977-859d-73af3846e8cc  <Site design Title>       {45116cea-2abb-4cf9-98da-ecd131cdf55c}

Complete Scripts:

Connect-PnPOnline -AppId "<Azure AppId>" -Url "<SharePoint admin URL>" -AppSecret "<Azure App client secret key>"

$content = Get-Content "<Path of JSON file>" -Raw

Add-PnPSiteScript -Title "<Script Title>" -Content $content

Id      		   Title                 Version Content
45116cea-2abb-4cf9-98da-ecd131cdf55c <Script Title> 0

Add-PnPSiteDesign -Title "<Title of my custom site design>" -SiteScriptIds "45116cea-2abb-4cf9-98da-ecd131cdf55c” -WebTemplate 68

Id            		   Title           		Site Scripts
5fde7e54-0e84-4977-859d-73af3846e8cc  <Site design Title> 	{45116cea-2abb-4cf9-98da-ecd131cdf55c}

References:

  1. Connect-PnPOnline
  2. PnP PowerShell overview
  3. Office 365 – PowerShell script to add / create custom site design to our Tenant
  4. Office 365 – Azure Active Directory – Registering/Creating new Azure App – detailed steps
  5. Office 365 – Microsoft Graph – Part 2 – Granting permission to Azure Apps to use Microsoft Graph APIs using CSOM
  6. SharePoint site design and site script overview
  7. Get-Content
  8. Add-PnPSiteScript
  9. Add-SPOSiteDesign

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.

Prasham Sabadra

LIFE IS VERY BEAUTIFUL :) ENJOY THE WHOLE JOURNEY :) Founder of Knowledge Junction and live-beautiful-life.com, Author, Learner, Passionate Techie, avid reader. Certified Professional Workshop Facilitator / Public Speaker. Scrum Foundation Professional certificated. Motivational, Behavioral , Technical speaker. Speaks in various events including SharePoint Saturdays, Boot camps, Collages / Schools, local chapter. Can reach me for Microsoft 365, Azure, DevOps, SharePoint, Teams, Power Platform, JavaScript.

You may also like...

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

%d bloggers like this: