How to Apply Site template (Site Design) via Power Automate

ApplySiteDesignFeatured

How to Apply Site template (Site Design) via Power Automate

In how to Apply Site template (Site Design) via Power Automate, I will demonstrate how to do it for the existing modern SharePoint site. Site templates, known in the past as Site Design, allows creating a set of the actions which can be applied to your SharePoint site. Actions like:

  • Apply color Theming
  • Run/Trigger Power Automate Cloud Flow
  • Apply a site logo
  • Installing a deployed solution from the app catalog like SPFx (SharePoint Framework) web parts

For me, it is very important for all SharePoint sites to keep the company branding. This is part of company identity, and I prefer to keep it consistent. I notice that this consistency might be broken when Users are creating Microsoft Teams team. After creation of the team, a new SharePoint Team site is also provisioned (all the Files from channels are stored in SharePoint at the end of the day). Thous I’m using it in the custom provisioning process which is creating new MS Teams and then is executing Site template (Site Design) actions.

I see multiple scenarios where this method may be used. Let me know your ideas.

NOTE: The Site Designs has been renamed to Site templates but in the REST API method keep the old name, so I made the decision to use both terms/names.

NOTE: Method won’t work for the Sites created as the Teams Private Channels.


Table of content:


Applying Site template (Site Design) Flow

Configuration

I created simple Flow with the Flow buttons as the trigger (Instant type). Secondly, to the trigger added two text input parameters:

  • SiteAddress
  • SiteDesignID

Next action which I added is Send an HTTP Request to SharePoint.

If you see this action for the first time, I’m thinking about written a quick article which can help to understand this action better. Let me know your thoughts.

I configured it in the following way:

  • Site Address: the value of the text input SiteAddress from the trigger
  • Method: selected POST
  • Uri:
_api/Microsoft.SharePoint.Utilities.WebTemplateExtensions.SiteScriptUtility.ApplySiteDesign
  • Headers: N/A
  • Body:
{
'siteDesignId': 'SiteDesignID',
'webUrl':'SiteAddress'
}

NOTE: In the code above I use the literal values for the presentation purpose but it you want to copy my actions step by step you should paste below lines:

{
'siteDesignId': '@{triggerBody()['text_1']}',
'webUrl':'@{triggerBody()['text']}'
}

NOTE: triggerBody()[‘text_1’] and triggerBody()[‘text’] corresponds to the text input values from the trigger SitesDesignId and SiteAddress, text_1 and text are the internal names.


Execution

I’m now ready for the first time to execute the Flow. I will run it again the Leadership Connection site:

My Site template (Site Design) is simple, it is only adding custom theme to the site. I’m adding values for the input properties and running the flow.

I’m working on the second post around the Site template and Power Automate to demonstrate how to get the GUID value for SiteDesignID. Stay tuned!


Result

Result of the execution is as follows (maybe not the best color but good for testing ๐Ÿ˜).

I’m also checking Flow result, including the Outputs of the action:

It is important to check that. As during working with the Site template (Site Design) I notice that action from Power Automate perspective will run successfully, but actually it failed… I mean, the Site template REST API call failed.

Bonus time: you can download this Flow in my GitHub ๐Ÿ˜ƒ just click here.


Applying Site template (Site Design) Flow with Error handling

I feel that closing how to Apply Site template (Site Design) via Power Automate at this stage will be a mistake, so please bear with me. I will now demonstrate how to add the handling of the Error/s from the REST API results.

TIP
The method described below can be re-used also for other REST API calls. The key is to understand the returned properties of the results.
Ps. Let me know if I should create a more generic post about it.

As per attached screenshot, Flow is now bigger. I think it is nicely showing how the production Flows are done ๐Ÿ˜œ

  • Init varResults: initialization of the variable of the array type
  • GetOnlyErrors: Filter Array action
  • Condtion: which is checking if the Filter Array (GetOnlyErrors) have results [I’m working under assumption that I want to deliver notification to Admin about failed steps / actions in Site template]
  • Select: clean up of unwanted columns
  • Create HTML table: will be used to present Errors array, instead of it the Create CSV table can be also used
  • Send an email (V2): with the details of the Error

I will now go step by step, explaining all the configurations made.


Init varResults

Ok, so what I did here? Instead of trying to Parse JSON or perform any other operation, I thought to create a new array as a variable. I made such a call because the results which are returned from REST API execution are in the array (as per attached screenshot). It is logical as Site template (Site Design) can contain more than one action to be executed.

NOTE: Each action in the Site template (Site Design) is called Site Script.

array(
outputs('Send_an_HTTP_request_to_SharePoint')?['body']
?['d']
?['ApplySiteDesign']
?['results']
)

I try to explain above code:

First I looked into the output of the action and step by step accessed the wanted area (results node). The outputs(‘Send_an_HTTP_request_to_SharePoint’)?[‘body’] is provided from Dynamic Content by default. Remaining parts is going via JSON tree ๐Ÿคฉ

TIP
Never forget about question mark (?) in such cases. It will prevent your process from breaking! If the value is not present or it is null process will not fail.


GetOnlyErrors

I just added Filter Array action and renamed it. The value of From is the varResults variable (array). Interesting is what is happening in the condition:

I clicked the left box and changed the dynamic content to Expressions and typed:

item()?['ErrorCode']

Again, I know what to type in from the preview of the Flow execution and Send and HTTP request to SharePoint action outcome. I’m checking if the ErrorCode is different from 0 as zero means everything was fine.

Result of GetOnlyErrors is array with Errors only ๐Ÿ˜


Condition

Next action condition is mandatory for me as I want to send notification only when REST API did not work.

In the condition I’m checking if GetOnlyErrors have value / is not empty:

empty(body('GetOnlyErrors'))

Empty function return value true or false. False is result, which means that there are Errors. In my condition, I compare the result of empty function with false value. If the result of the check is true, so the array is not empty (Errors found) I carry on my Flow actions to notify administrator.


Select

In the Select action I’m removing all the properties which are not important for me. Keeping only Error Code, Title and the Error text (Outcome):

For each item in the Map section of Select action Im adding the Key name and in the value typing expressions to match the values which I want to extract from Array:

item()?['ErrorCode']
item()?['Title']
item()?['OutcomeText']

Create HTML table

To a Create HTML table action, I add Select Output value. This action can be replaced with Create CSV table.


Send an email (V2)

Except the Output of the Create HTML Table I’m adding to the body of the email also expression which will open for Administrator link to the run history of the flow which failed.

Bonus: Link to the failed Flow run

Personally I use this URL a lot and if the Flow is failing I don’t need to browse in the list of historical runs, just have it in my email! ๐Ÿ˜

<a href=https://unitedkingdom.flow.microsoft.com/manage/environments/@{workflow()['tags']['environmentName']}/flows/@{workflow()['name']}/runs/@{workflow()?['run']?['name']}>Open flow @{workflow()['tags']['flowDisplayName']}</a>

TIP
You can copy past the expression and use it in your Flows just check what is before .flow.microsoft.com. I’m using tenant in unitedkingdom region but your might be emea or us or other.

Bonus time: you can download this Flow from my GitHub ๐Ÿ˜ƒ just click here.


Summary

I hope how to Apply Site template (Site Design) via Power Automate will be useful for you. I’m using this method a lot, not only to add the custom branding to the sites.

I’m keen to understand how you are using this method.

Additionally, I’m very happy that decided to add the Error Handling part to this post, thou making it twice as long as it was initially… Yet I feel it is adding extra value to the whole post and wonder if I should extract it and make it more generic as it is a nice way to handle the results of the REST API calls. Let me know, please.

Bonus 1 of this post is the URL which will direct you to the history page of the Flow run, I just love this URL ๐Ÿคฉ hope you will to.

Bonus 2 you can download both Flows presented in blog from my GitHub.

Last but not least if you are less advance User and you are filling intimidated by this content please let me know how I can improve it to help you and other to learn easier.

Thanks for reading and have a great day ๐Ÿ˜Š

One thought on “How to Apply Site template (Site Design) via Power Automate

Leave a Reply

Your email address will not be published. Required fields are marked *