Applying site scripts to modern site collections using Power Automate

Introduction

As more and more businesses are turning towards modern site collections for better collaboration and document management, it becomes essential to streamline the processes that go into creating, maintaining, and updating these sites. One way to automate this process is using Power Automate to apply site scripts to modern site collections.

Site Scripts and Site Designs

Site scripts are JSON files containing instructions for applying custom configurations to a site collection. They can be used to add custom branding, create site columns, configure site permissions, and more.

Site designs are a way to bundle site scripts together and apply them to a modern site collection. Site designs allow you to use consistent configurations for multiple site collections, saving you time and effort.

Power Automate

Power Automate is a cloud-based service that allows you to automate tasks and workflows across multiple applications and services. It provides a user-friendly interface for creating and managing workflows triggered by events, such as adding items to a SharePoint list or a new email in your inbox. Power Automate can be used to apply site scripts to modern site collections, enabling you to automate configuring your site collections.

Prerequisites

Before we begin, there are a few prerequisites that must be met:

  • It would be best to have a modern site collection in SharePoint Online.
  • You must have permission to create and manage site designs in your organization.
  • You must have a Power Automate account with the appropriate permissions to create and manage workflows.

To create a site script using the SharePoint Online Management Shell, follow these steps,

  1. Open the SharePoint Online Management Shell and connect to your SharePoint Online tenant.

  2. Run the following command to generate a site script based on an existing site collection:

In this article, we will guide you through applying site scripts to modern site collections using Power Automate.

$site = Get-SPOSite https://<your-site-collection-url>
$script = $site.GetSiteDesign(0).SiteScript

Replace <your-site-collection-url> with the URL of your site collection.

   3. Save the output of the command to a JSON file.

$script | Out-File -FilePath C:\site-script.json

This will save the site script to a file called site-script.json on your local machine.

Alternatively, you can create a site script using a text editor. The site script must be a valid JSON file that conforms to the site script schema. You can find more information on the site script schema in the Microsoft documentation.

Once you have created your site script, you can use Power Automate to apply it to your modern site collection.

Step 1. Create a site script

The next step is to create a site script. The site script defines the customizations that need to be made to the SharePoint site. Site scripts are written in JSON format and can be created using any text or JSON editor.

Here is an example of a site script that creates a new list called "New List".

{
    "$schema": "schema.json",
    "actions": [
        {
            "verb": "createSPList",
            "listName": "New List",
            "templateType": 100
        }
    ],
    "bindata": { },
    "version": 1
}

In this example, the "createSPList" action creates a new list with the name "New List" and the template type 100 (which corresponds to a custom list).

Step 2. Creating a Site Design

The next step is to create a site design, which is a way to bundle site scripts together and apply them to a modern site collection. Site designs allow you to apply consistent configurations to multiple site collections.

To create a site design using Power Automate, follow these steps,

  1. Sign in to the Power Automate portal.
  2. Click the "Create" button in the top navigation bar, then select "Automated cloud flow".
  3. Give your flow a name and select "Create from blank".
  4. Select the "Manually trigger a flow" trigger in the trigger step.
  5. In the next step, add an "HTTP" action.
  6. In the "HTTP" action, set the method to "POST" and the URI to the following:
  7. https://<your-tenant-name>.sharepoint.com/_api/Microsoft.Sharepoint.Utilities.WebTemplateExtensions.SiteScriptUtility.CreateSiteDesign
    
  8. Replace <your-tenant-name> with the name of your SharePoint Online tenant.
  9. Set the "Body" field to the contents of your site script. You can either paste the contents of your site script directly into the "Body" field, or you can reference a JSON file that contains your site script by using the "Parse JSON" action.
  10. Set the "Content-Type" field to "application/json".
  11. Add an "Authorization" header to the action, and set the "Bearer" token to a valid access token for your SharePoint Online tenant. You can generate an access token by following the instructions in the Microsoft documentation.
  12. Add a "Response" action to the flow, and set the "Body" field to the output of the "HTTP" action. This will allow you to see the response from the SharePoint API and verify that the site design was created successfully.
  13. Save and run your flow.

Once your flow has been completed successfully, your site design will be available in the site design gallery of your SharePoint Online tenant. You can apply the site design to a modern collection by navigating to the site collection settings and selecting "Site designs". You can select your site design from there and apply it to your site collection.

Conclusion

In this article, we have explored how to apply site scripts to modern site collections using Power Automate. Site scripts are a powerful tool for customizing and configuring your SharePoint Online site collections. Site designs allow you to apply consistent configurations to multiple site collections. Using Power Automate to automate the process of applying site scripts and site designs, you can save time and effort and ensure that your site collections are configured correctly and consistently.