Microsoft Flow – Create a new sub site using Microsoft Flow!

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 the more exciting bit of how to create a new 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 MSDN site.

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

Microsoft Flow – Create a new sub site using Microsoft Flow!

Microsoft Flow – Create a new sub site using Microsoft Flow!

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.

Creating a site

This time we need to use the POST method.

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

Then once again set the headers.

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.

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.

Simple text feeding

Simple text feeding

Now run your flow and your site will be created!

Success Handling

When you created a 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://pieterveenstratriaddev.sharepoint.com/_api/web/webinfos/add”,
      “uri”: “https://pieterveenstratriaddev.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
  }
}
Initialize variable

Initialize variable

Error handling

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

Error handling

Error handling

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://pieterveenstratriaddev.sharepoint.com/_api/web/webinfos/add”,
      “errors”: [
        “-2147024713”,
        “Microsoft.SharePoint.SPException”
      ]
    }
  }
}

Where can I use this?

Just to give you a few ideas.

  • 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.

At Triad I implemented some of these site creation processes in the past. But it was never this easy as it is with Microsoft Flow.

About the Author: Since 2007 I’ve worked with SharePoint as a Consultant, Developer and Architect. I’m currently working HybrIT Services (hybrit.co.uk). In 2017 Microsoft has awarded me with an MVP Award in the category Office Servers and Services.

Reference: Veenstra, P (2018). Microsoft Flow – Create a new sub site using Microsoft Flow! Available at: https://sharepains.com/2018/05/02/microsoft-flow-create-a-new-sub-site-using-microsoft-flow/ [Accessed 22 June 2018]

Share this on...

Rate this Post:

Share: