Get Existing Site Template And Apply To Existing SharePoint Site

Introduction

Many times, a site is already created, and we need to apply configuration in the existing site to achieve some level of consistency. How can we achieve it?

To achieve this kind of implementation, use site template and site scripts to automate provisioning to the existing modern site or create a new site using site templates which is available in your organization tenant.

In the previous blog, I cover how to create a new site Script and Site Template using Rest API in Power Automate.

I hope, you have already created the Site Script and Site Design. If you want to create then you can click here.

Today we are looking at how developers can get an existing Site Template and Apply it to SharePoint's existing site using Rest in Power Automate.

Learning Objectives

  1. Get Site Template
  2. Filter Site Template by Name
  3. Apply Site Template to existing SharePoint Site.

Prerequisite

  1. M365 environment
  2. Power automate access requires a license
  3. SharePoint Admin Access
  4. Site Template in tenant

Let's get started,

Create Power automate

Go to https://portal.office.com and click on Power automate.

Click on “New Flow” and Select “Instant Cloud Flow”.

Select “Manually Trigger Flow” and Add Name “Create-Site Template-Flow”.

Click on Create Button.

Get Existing Site Template and Apply to Existing SharePoint Site

Get existing Site Template from Tenant

Click on “Add Action”.

Select “Send an HTTP request to SharePoint”.

Select Site from List.

Select Method “Post”.

Add below URL to URI section.

/_api/Microsoft.SharePoint.Utilities.WebTemplateExtensions.SiteScriptUtility.GetSiteDesigns

Click on “Add Action”.

Select “Parse JSON”.

Add below JSON to Parse JSON.

{
    "type": "object",
    "properties": {
        "d": {
            "type": "object",
            "properties": {
                "GetSiteDesigns": {
                    "type": "object",
                    "properties": {
                        "__metadata": {
                            "type": "object",
                            "properties": {
                                "type": {
                                    "type": "string"
                                }
                            }
                        },
                        "results": {
                            "type": "array",
                            "items": {
                                "type": "object",
                                "properties": {
                                    "Description": {
                                        "type": "string"
                                    },
                                    "DesignPackageId": {
                                        "type": "string"
                                    },
                                    "DesignType": {
                                        "type": "string"
                                    },
                                    "IsDefault": {
                                        "type": "boolean"
                                    },
                                    "IsOutOfBoxTemplate": {
                                        "type": "boolean"
                                    },
                                    "IsTenantAdminOnly": {
                                        "type": "boolean"
                                    },
                                    "ListColor": {
                                        "type": "string"
                                    },
                                    "ListIcon": {
                                        "type": "string"
                                    },
                                    "PreviewImageAltText": {},
                                    "PreviewImageUrl": {},
                                    "RequiresGroupConnected": {
                                        "type": "boolean"
                                    },
                                    "RequiresTeamsConnected": {
                                        "type": "boolean"
                                    },
                                    "RequiresYammerConnected": {
                                        "type": "boolean"
                                    },
                                    "SiteScriptIds": {
                                        "type": "object",
                                        "properties": {
                                            "__metadata": {
                                                "type": "object",
                                                "properties": {
                                                    "type": {
                                                        "type": "string"
                                                    }
                                                }
                                            },
                                            "results": {
                                                "type": "array",
                                                "items": {
                                                    "type": "string"
                                                }
                                            }
                                        }
                                    },
                                    "SupportedWebTemplates": {
                                        "type": "object",
                                        "properties": {
                                            "__metadata": {
                                                "type": "object",
                                                "properties": {
                                                    "type": {
                                                        "type": "string"
                                                    }
                                                }
                                            },
                                            "results": {
                                                "type": "array"
                                            }
                                        }
                                    },
                                    "TemplateFeatures": {
                                        "type": "object",
                                        "properties": {
                                            "__metadata": {
                                                "type": "object",
                                                "properties": {
                                                    "type": {
                                                        "type": "string"
                                                    }
                                                }
                                            },
                                            "results": {
                                                "type": "array"
                                            }
                                        }
                                    },
                                    "ThumbnailUrl": {},
                                    "Title": {
                                        "type": "string"
                                    },
                                    "WebTemplate": {
                                        "type": "string"
                                    },
                                    "Id": {
                                        "type": "string"
                                    },
                                    "Order": {},
                                    "Version": {
                                        "type": "integer"
                                    }
                                },
                                "required": [
                                    "Description",
                                    "DesignPackageId",
                                    "DesignType",
                                    "IsDefault",
                                    "IsOutOfBoxTemplate",
                                    "IsTenantAdminOnly",
                                    "ListColor",
                                    "ListIcon",
                                    "PreviewImageAltText",
                                    "PreviewImageUrl",
                                    "RequiresGroupConnected",
                                    "RequiresTeamsConnected",
                                    "RequiresYammerConnected",
                                    "SiteScriptIds",
                                    "SupportedWebTemplates",
                                    "TemplateFeatures",
                                    "ThumbnailUrl",
                                    "Title",
                                    "WebTemplate",
                                    "Id",
                                    "Order",
                                    "Version"
                                ]
                            }
                        }
                    }
                }
            }
        }
    }
}

Filter Site Template By Name

Select “Condition” from the list of connectors.

Compare Title to Site Template Name. The title will be returning from Parse JSON action.

When you select Title from it, It would convert in Foreach loop because Get Site Template returns all site templates from the tenant.

Get Existing Site Template and Apply to Existing SharePoint Site

If Condition is “True” then we will apply the Site Template to the Existing Site.

Apply Site Template to existing SharePoint Site

Click on “Add Action” inside the True Section of Condition.

Select “Send an HTTP request to SharePoint”.

Select Site from the list.

Select Method “Post”.

Add Below URL-to-URL section.

/_api/Microsoft.SharePoint.Utilities.WebTemplateExtensions.SiteScriptUtility.ApplySiteDesign

The header Should be as below.

{
  "Content-Type\n": "application/json;charset=utf-8",
  "accept": "application/json;odata.metadata=minimal",
  "odata-version": "4.0"
}

The body Should be as below.

{
 "siteDesignId": "@{items('Apply_to_each')?['Id']}", // ID from existing item in from For each loop
 "webUrl":"https://dev1802.sharepoint.com/sites/Demo3" // Site where you want to apply site template
}

Full Flow would be like as below.

Get Existing Site Template and Apply to Existing SharePoint Site