Create a new sub site using Power Automate!

Today we have seen an exciting new action added to Microsoft Flow! The action is called Send an HTTP request to SharePoint.

In this post I’m going to have a look at how to get site details out of SharePoint and after that I will look at the more exciting bit of how to create a new sub site. This will open many doors to site request processes in Office 365. Imagine creating a site request list, go through an approval process and create your team site all within no time at all.

Get Site details

I’m going through this step just to explain the basics.

Once you have found the Send an HTTP request to SharePoint action, all you have to do is supply the details to the REST API endpoints. Details can be found on the Microsoft Docs site.

Ok, so the Uri to be used is simply /_api/web

List the details of a site using the HTTP request to SharePoint

don’t forget to set the header to Accept=application/json;odata=verbose

Ok, this was easy. When you now look at the run output from your flow you will find ll the details of the site in json format!

Now the more exciting bit.

Create a new sub site

This time we need to use the POST method to call our SharePoint REST API.

the uri now is /_api/web/webinfos/add

Then once again set the headers. In this case you don’t need to use the verbose option. You could use nometadata instead however I have got used to the extra bits needed in the body when using the verbose option.

Accept=application/json;odata=verbose

Content-Type=application/json;odata=verbose

and the Body needs to be set to something like this:

{ 'parameters':
  { '__metadata':
    { 'type': 'SP.WebInfoCreationInformation' },
    'Url':'testsiteflow2',
    'Title':'My Title2',
    'Description':'My Description',
    'Language':'1033',
    'WebTemplate':'STS#0',
    'UseUniquePermissions':false
  }
}

The Url in the above json will be the url used within the site give in the Site Address to create a new sub site. So you would most likely use some dynamic content to set this Url.

The template I’ve set to STS#0.  This will give you a team site but of course you could select any of the other templates. But imagine creating modern team sites. Yes you can do this by just updating the WebTemplate in the above to STS (without the #0)

Do you still remember creating dictionaries in SharePoint Designer? Just to get this one call done you had to create 4 of those dictionaries. Now we’ve just just plain simple text feeding the site creation process.

Create a new sub site using Microsoft Flow

Now run your flow and your site will be created!

Success Handling

When you created a new sub site you could set a variable of the type Object to the Body that is returned by the HTTP request action. This will return something like this:

{
  "d": {
    "__metadata": {
      "id""https://pieterveenstramvp.sharepoint.com/_api/web/webinfos/add",
      "uri""https://pieterveenstramvp.sharepoint.com/_api/web/webinfos/add",
      "type""SP.WebInformation"
    },
    "Configuration"0,
    "Created""2018-05-02T16:37:28",
    "Description""My Description",
    "Id""26cad790-c071-4d82-acba-e443620b87b3",
    "Language"1033,
    "LastItemModifiedDate""2018-05-02T16:37:51Z",
    "LastItemUserModifiedDate""2018-05-02T16:37:51Z",
    "ServerRelativeUrl""/testsiteflow3",
    "Title""My Title2",
    "WebTemplate""STS",
    "WebTemplateId"0
  }
}
successHandling

Error handling

When things go wrong however you need to handle things to. This starts by setting the run after settings.

Run after settings

Then you will get a BadGateway error returning the following json

{
  "error": {
    "code"502,
    "source""europe-001.azure-apim.net",
    "clientRequestId""861c4a53-f91b-4945-ae71-3335403eb634",
    "message""BadGateway",
    "innerError": {
      "status"502,
      "message""The Web site address \"/testsiteflow3\" is already in use.\r\nclientRequestId: 861c4a53-f91b-4945-ae71-3335403eb634\r\nserviceRequestId: 7b3e639e-70e7-5000-de78-8990577b77ff",
      "source""https://pieterveenstramvp.sharepoint.com/_api/web/webinfos/add",
      "errors": [
        "-2147024713",
        "Microsoft.SharePoint.SPException"
      ]
    }
  }
}

Where can I use this?

Just to give you a few ideas. There are of course a lot more.

  • You could create project sites as projects are registered in your PMO solution. Or
  • you could create a Client site as your new client has been added to CRM.
  • Or maybe create a site after a survey in Microsoft Forms has been completed? The options are endless.

Discover more from SharePains

Subscribe to get the latest posts sent to your email.

Related Posts

Leave a Reply

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