Applying a Site Template via SharePoint API call is not proving reliable

Iron Contributor

 

I  have been working on the ablity to create SharePoint sites via a Power App and Power Automate

 

With a Send an HTTP Request to SharePoint Action you can apply a site template in a POST:

 

 

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

 

  

The body would like 

 

{"siteDesignId": "SiteDesignId", "webUrl":"@{outputs('SharePoint_Site')?['body/siteurl']}"}

 

 

The site script:

{
    "$schema": "https://developer.microsoft.com/json-schemas/sp/site-design-script-actions.schema.json",
"actions": [
    {
        "verb": "addContentTypesFromHub",
        "ids": ["0x01010xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "0x0120xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "0x0101xx" ]
    },
    {
       "verb": "createSPList",
       "listName": "Documents",
       "templateType": 101,
       "subactions": [
           {
               "verb": "addContentType",
               "name": "ContentType_1"
           },
           {
            "verb": "addContentType",
            "name": "ContentType_2"
           },
           {
               "verb": "addContentType",
               "name": "Content_Type3"
           },

What am seeing is when I apply the same template though the browser UI it works reliably but via 

the Power Automate, say when batching these requests.  Essentially,  I am seeing content types not added to my document library or the default content type not being set.

 

Anybody else experienced this?

 

 

1 Reply

@Daniel Westerdale 

It's possible that the issue you're experiencing is related to the timing of the API calls or the order in which they are executed. When you apply a site template via the SharePoint API, it may take some time for all the actions in the site script to complete. If subsequent API calls are made before the previous actions have completed, it can cause issues with the site design.

One solution you can try is to use the "waitForJson" method in the SharePoint REST API to wait for the completion of each action before executing the next one. This method allows you to specify a delay time and a maximum number of retries to wait for the completion of an action before timing out.

Here's an example of how you can modify your Power Automate flow to use the "waitForJson" method:

1. After applying the site design, add a "Delay" action with a reasonable amount of time (e.g., 30 seconds) to allow for the completion of the actions in the site script.

2. After the "Delay" action, add a "Send an HTTP request to SharePoint" action to check the status of the site design using the "waitForJson" method. The request URL should be in the following format:

_api/Microsoft.SharePoint.Utilities.WebTemplateExtensions.SiteScriptUtility.GetSiteDesignRunStatus(id='{siteDesignRunId}')

Replace {siteDesignRunId} with the site design run ID returned by the "Apply site design" API call.

3. In the headers of the "Send an HTTP request to SharePoint" action, add the following:

Accept: application/json;odata=nometadata
Content-Type: application/json;odata=nometadata

 

4. In the body of the "Send an HTTP request to SharePoint" action, add the following JSON:

 

{
"method": "POST",
"headers": {
"X-HTTP-Method": "GET",
"If-Match": "*"
},
"body": {
"retryInterval": 1000,
"maxRetry": 30
}
}

This JSON specifies a retry interval of 1 second and a maximum number of retries of 30, which means that the action will retry every second for up to 30 seconds until it receives a response.

5. After the "Send an HTTP request to SharePoint" action, add a "Parse JSON" action to parse the response and extract the "state" property. This property indicates the current status of the site design run.

6. Add a condition that checks whether the "state" property is equal to "Completed". If it is, proceed to the next action in your flow. If it is not, loop back to step 2 and check the status again.

By using the "waitForJson" method and adding a delay between API calls, you can ensure that the site design actions are completed before making subsequent API calls. This should help to avoid issues with content types not being added or the default content type not being set.

If I have answered your question, please mark your post as Solved
If you like my response, please give it a like