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

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.

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
  }
}

Error handling

When things go wrong however you need to handle things to. This starts by setting the 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.

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

Share
Pieter Veenstra

Business Applications and Office Apps & Services Microsoft MVP working as a Microsoft Productivity Principal Consultant at HybrIT Services. You can contact me using contact@veenstra.me.uk.

View Comments

  • Hi Pieter using REST calls within Flow is a game changer ! - No more SharePoint Designer Workflows as you can do almost everything with REST calls ! Hurrah !

    • Hi Nigel,

      That is right! I stopped using SPD for a while now. The only need for SPD is in the on-premises world.

  • Hi Pieter,
    I'm not finding Send an HTTP request to SharePoint action in available actions in Flow, do you know if this is only available for early adopters?

    Thanks,

    Art

    • Hi Art,
      It only arrived yesterday for me. I do have targeted release switched on. So you might have to wait a few days for this to arrive.

  • Hier Pieter , did you try to create a modern site? would it be different (given that it is a site collection?

    • HI Dominique,

      I haven't tried creating site collections yet. It would be different to create a site collection. Traditional sites you can create with the admin service. I guess creating new team sites you could do with the teams connector.

  • Any luck getting Flows to run on subsites? They don't show up for me, am assuming subsites aren't supported.

    • Hi chad, you mean having a trigger start on item creating within lists in a subsite? It wokrs for me so it should be ok. You might have to add the url of the subsite by adding the url manually rather than selecting it form the drop down.

  • Hello Pieter
    Would you know the what I have to put in so that the top link navigation is inheritance is activated

    Thanks

    Miles

  • Thanks for this, really useful. Is it possible to set permissions on the Subsite and not inherit from the parent?

  • Hi All,
    I've tried the code above in MS Flow several times now, and the flow is FAILING with the following output returned in the HTTP Response:

    "status": 400,
    "message": "The property '__metadata' does not exist on type 'SP.WebInfoCreationInformation'. Make sure to only use property names that are defined by the type.\r\nclientRequestId: b4e4d5fe-0d1c-425e-bf6d-fb1a525d6c4b\r\nserviceRequestId: 04c9819e-b003-6000-20af-c856f48a57c6",
    "source": "https://ieaonline.sharepoint.com/sites/testsc/_api/web/webinfos/add",
    "errors": [
    "-1",
    "Microsoft.Data.OData.ODataException"
    ]

    NO ONE else seems to be having this less-than-ideal experience I'm having :-(

    Thoughts?

    -Mark in North Aurora IL

    • HI Mark, When I hit these kind of problem I often go to the REST API url in my browser. Quite often it then shows the fields names etc.

  • SOLVED recently-reported problem (getting HTTP 404 error):
    CAUSE: I neglected to add the "Content-Type" HTTP Request Header variable in my HTTP POST request, that's all :-)

    WORKING now, and I'm with the others who commented:
    - MSFlow is MORE than a replacement for SPD Workflows :-)
    - MSFlow is true SOA-styled SCALABLE, ROBUST workflow for automation :-)
    - MSFlow can be MOBILIZED readily (talk about CODE RE-USE !) :-)
    and
    - THIS SendHTTPRequest Action was the codeless tool we've been waiting for in MSFlow !

    TO PONDER...
    The whole philosophy behind MSFlow is that it's "code-less"; but I find it ironic that in order to fully UNDERSTAND MSFlow... many users often times find themselves UNDERSTANDING "CODING" better than they EVER did (or had to) when doing "pure coding" ;-)

    All MSFlow really does is effectively "VISUALIZE" or "DIAGRAM-IZE" your coding efforts, as opposed to force you to MENTALLY VISUALIZE your coding.

    MSFlow truly MERGES the "textual" and the "visual" aspects of understand program execution, PARTICULARLY in a distributed SOA-based environment where there are truly generalized "clients" consuming "services".

    IF anyone is old enough to remember the 20+ YEARS back to MACROMEDIA, and its DIRECTOR interactive application design environment,, you'll see how far (or how LITTLE) we've actually progressed in more than 2 decades; I guess we simply got distracted by this construct now called "the INTERNET" ;-) ...

    - Mark

    • I'm glad you got things resolved. If you need any further help feel free to contact me.

  • Hi ,
    Flow works cool !!!
    how to add global navigation Display the same navigation items as the parent site
    using JSON code please advice ..

1 2 3

Recent Posts

Calculate the Sum for a SharePoint column in Power Automate

Last week Shane Young asked me about calculating the Sum for a SharePoint column in…

2 days ago

Object must implement IConvertible in Power Apps

In Power Apps when you do a Patch to create a new item or to…

6 days ago

Get started with adaptive cards in Power Automate

Getting started with adaptive cards can be difficult. In this post i have written some…

2 weeks ago

Create PDF documents from data in Power Automate

In this post I will look at how to create PDF documents from data. Use…

2 weeks ago

Unnest nested arrays in Power Automate

We all know this problem, you have a nested array in Power Automate but how…

3 weeks ago

Advanced settings not loading in Power Platform

Yesterday one of my clients showed me an issues where the Advanced settings didn't load.…

4 weeks ago
%%footer%%