Create a SharePoint site using REST in workflow with SharePoint Designer

SharePoint 2013 Workflows does not have any actions for creating a SharePoint sites, but you can solve this using the «call http web service» action and SharePoint REST API.

I wanted to give the users a form to order new sites in an easy way without being a site owner. I wanted to solve this using a SharePoint list with an attached workflow as a site order form. I am doing this proof of concept on my lab-site http://intranet.wingtip.com.

  1. Enable Workflow can use app permissions
  2. Create a list for ordering sites
  3. Create a workflow that creates a site when new item is created
  4. Grant full control permission to workflow (needed for workflow to create site) 

Enable Workflow can use app permissions

  1. Under Settings button
  2. Select Site settings
  3. Select Manage site features
  4. Click Activate on Workflows can use app permissions


Create a list for ordering sites

  1. From your site – click Site Contents
  2. Click Add an app
  3. Click Custom list
  4. Give the new list a name like «Order project site» and click OK

We do not need any more fields for this proof of concept.

Create the workflow

  1. Click Order project site in the left menu to open your list
  2. Click List in the top menu to open list settings
  3. Click Workflow Settings in the toolbar, then click Create a Workflow in SharePoint Designer

  4. Click Allow if you get a security warning
  5. Enter a name for your workflow and click OK

  6. Rename the Stage to «Creating request header»
  7. Add a Build Dictionary action
  8. Name the output variable «RequestHeader»:

  9. Click this to add variables to the dictionary
  10. Click Add
  11. Add a string variable named content-type with the value application/json;odata=verbose and click OK

  12. Add a new string variable named Accept with the value application/json;odata=verbose and click OK

  13. Click OK

  14. Click Stage in the toolbar to create a new step
  15. Rename the stage to Creating Request Body
  16. In Stage: Creating request header set Transition to stage to Go to Creating Request Body
  17. In Stage: Creating Request Body add a new Build Dictionary action
  18. Set output to a new variable MetaData
  19. Add string variable named type with value SP.WebInfoCreationInformation

  20. Click OK to confirm all open dialog boxes
  21. Add a new Build dictionary action
  22. Set output to a new variable named RequestHeader
  23. Add these variables to RequestHeader:
Name Type Value
Url String Currentitem: TitleClick the fx button to add the Title of the current list item:
Title String Currentitem: TitleClick the fx button to add the Title of the current list item:
Description String Currentitem: TitleClick the fx button to add the Title of the current list item:
Language Integer 1033
WebTemplate String sts#0
UseUniquePermissions String false
__metadata Dictionary Variable: MetaDataClick the fx button to add the previous created MetaData variable:
  1. Click OK in all dialog boxes to save RequestHeader dictionary variable
  2. Add a new Build dictionary action
  3. Set output to new variable named RESTparameters
  4. Add a variable named parameters type Dictionary
    with the value of the previous created RequestHeader

  5. Click OK to close all open dialogs
  6. Add a new Stage and rename it to Creating Site
  7. Set Transition to stage in Creating Request Body to go to Creating Site
  8. In Stage: Creating Site add App Step
  9. In App step add Call http web service action
  10. Click this to add the web service URL
  11. Enter the URL (use your own site and add /_api/web/webinfos/add to it) and select HTTP Post as HTTP method:

  12. Click OK when done
  13. Select the Call HTTP web service action
  14. Click Advanced Properies in the toolbar
  15. Set RequestHeaders to variable RequestHeder and set RequestContent to variable RESTparameters:

  16. Click OK to close dialog
  17. In Stage: Creating Site set Transition to stage to go to End of Workflow
  18. Click Publish
  19. Click OK in the next dialog:

  20. Click Create site just below the Create site tab:

  21. Check Start workflow automatically when an item is created

  22. Click Publish in the toolbar and confirm if you get any warning.

Grant full control permission to workflow

The following steps must be completed by a user that is a site owner.  These actions require that a workflow has been published to the site.

  1. Click on the Settings button
  2. Go to Site Settings
  3. Under Users and Permissions, select Site app permissions
  4. Copy the client section of the App Identifier. This is the identifier between the last «|» and the «@» sign.

    Example:

  5. Navigate to the Grant permission to an app page. This must be done by browsing to appinv.aspx in the site.
    http://{hostname}/{site}/_layouts/15/appinv.aspx

    Example:


  6. Paste the client id in the App Id field and click Lookup
    Example: 


    Example:
    You will see complete details of the workflow app id.

  7. Paste the following Permissions Request XML to grant full control permission
       1: <AppPermissionRequests>

       2:     <AppPermissionRequest

       3:          Scope="http://sharepoint/content/sitecollection"

       4:          Right="FullControl" />

     

       5: </AppPermissionRequests>

Example:


  1. Click Create. You will then be asked for consent. Verify that you are asked for consent to give workflow full control of the site. If you do not see full control consent request it probably means that there was an error in the permissions request XML.

Example:


  1. Click Trust It

Your list should now create a new site each time an item is added.

 

Reklamer

44 kommentarer om “Create a SharePoint site using REST in workflow with SharePoint Designer

  1. Thanks so much! In the Permissions Request XML, users should replace the slanted quotation marks with straight ones, as this may throw an error.

  2. Hi Roger I am facing issue while trying to do the exactly same as you have done in your POC.
    RequestorId: 1b8a5b1a-b322-5921-0000-000000000000. Details: An unhandled exception occurred during the execution of the workflow instance. Exception details: System.NotSupportedException: CannotReadAsType=Cannot read ‘Microsoft.Activities.Dynamic.DynamicObject’ as ‘System.String’ type. at Microsoft.Activities.Dynamic.DynamicItem.ReadAs(Type type) at Microsoft.Activities.Dynamic.DynamicItem.ReadAs[T]() at Microsoft.Activities.Messaging.SendHttpRequest.AddHeader(String headerName, DynamicItem headerValue, Boolean isContentHeader, HttpRequestMessage requestMessage, IList`1& contentHeaders, Boolean& isEmptyValue) at Microsoft.Activities.Messaging.SendHttpRequest.SendRequest(NativeActivityContext context) at System.Activities.NativeActivity.InternalExecute(ActivityInstance instance, ActivityExecutor executor, BookmarkManager bookmarkManager) at System.Activities.Runtime.ActivityExecutor.ExecuteActivityWorkItem.ExecuteBody(ActivityExecutor executor, BookmarkManager bookmarkManager, Location resultLocation)
    Can you please help me out what is wrong I have done.

  3. Hi Roger,
    I am able to solve the earlier error that I have posted. But when now I am calling the service to create the site getting the Forbidden access to call the service.
    So my site creation is failed. I have given the full control permission to my workflow. Please help me out on this.
    Regards,
    Sumit

    • I got wrong quotation marks in «Permission Request» on my posting. I’ve tried to correct this. Also check the comment from Jamie.
      Roger

    • Same for me. The complete Response is: {«error»:{«code»:»-1, Microsoft.Data.OData.ODataException»,»message»:{«lang»:»en-EN»»value»:»An unexpected ‘PrimitiveValue’ node was found when reading from the JSON reader. A ‘Start-Object’ node was expected».»}}}

  4. Tilbaketråkk: Create a SharePoint Site (SPWeb) using REST in SPD 2013 Workflow | SharePointRyan.com

  5. Tilbaketråkk: Create a SharePoint Site (SPWeb) using REST in SPD 2013 Workflow - The Microsoft SharePoint Blog

  6. Hello Roger,
    thanks for your clear instructions! Should this also work in SharePoint 2013 Online?

    Kind rgds,
    Yvonne

  7. Thanks Roger, this is brilliant! I’ve been showing it in some of my talks and giving you full credit, with links to this article. I hope to extend on it at some point. You have demystified a very powerful yet poorly documented set of capabilities – thanks again!!

  8. Thank you for your post, Is there a way to have the sub site created under a specific site. For example the workflow would be on the top level site and I already have three sites under the top level; company A, Company B, Company C. I want to fill out a form with info and one piece of info would be the Company that this sub site should go under (Company B). So the workflow runs and instead of adding the site under the home it adds it as a sub site under Company B. Is that possible and can you point me in the right direction if it is?

    Thank you,

  9. Hi, really good, been looking for this for a while. Im using sharepoint online, and I keep getting suspended during the workflow. with this error…..

    RequestorId: 767ee3a2-9194-b6cc-0000-000000000000. Details: An unhandled exception occurred during the execution of the workflow instance. Exception details: System.NotSupportedException: CannotReadAsType=Cannot read ‘Microsoft.Activities.Dynamic.DynamicObject’ as ‘System.String’ type. at Microsoft.Activities.Dynamic.DynamicItem.ReadAs(Type type) at Microsoft.Activities.Dynamic.DynamicItem.ReadAs[T]() at Microsoft.Activities.Messaging.SendHttpRequest.AddHeader(String headerName, DynamicItem headerValue, Boolean isContentHeader, HttpRequestMessage requestMessage, IList`1& contentHeaders, Boolean& isEmptyValue) at Microsoft.Activities.Messaging.SendHttpRequest.SendRequest(NativeActivityContext context) at System.Activities.NativeActivity.InternalExecute(ActivityInstance instance, ActivityExecutor executor, BookmarkManager bookmarkManager) at System.Activities.Runtime.ActivityExecutor.ExecuteActivityWorkItem.ExecuteBody(ActivityExecutor executor, BookmarkManager bookmarkManager, Location resultLocation) Exception from activity SendHttpRequest HttpPost Switch Sequence Microsoft.SharePoint.WorkflowServices.Activities.CallHTTPWebService Sequence Microsoft.SharePoint.WorkflowServices.Activities.AppOnlySequence Creating Site Sequence Flowchart Create Site.WorkflowXaml_6932297d_96fb_4989_abd6_854eed1bc385

    Not sure where to go with this.

    Thanks

  10. Hi Roger,

    Do you know how to construct request body for SOAP message (its envelope) using Sharepoint Designer 2013? I tried to put its envelope in a variable in a dictionary, but I got Bad Request.

    Thank you so much for your help.

  11. «Permission Request» what is that what was wrong in that.. i am getting unazthorized error. please help.
    please provide me exact permission request XML

  12. I love it! Very useful! Thanks so much for posting this.

    One question–how do I make the workflow use a custom site template?

    • Hi Tim

      Thanks for Your feedback!
      Custom site template will get a strange system name constructed of a guid/id + the name – you will need this to be able to use a custom template. You should be able to find the template name using PowerShell (I don’t remember the syntax right now, but if you can’t find it I’ll help you). I did this in JavaScript looping throug all names until I found my template and then using the system name for it to create my site. Should be possible to do the same in SharePoint Designer Workflow as well. I cannot Access my Development machine right now, but I can post the code later if you need it.
      Roger

  13. First of all thank you for this greate piece of work.
    I got one question:
    Is it possible to fill out contents on the new subpage within the workflow?
    Example: In my project page and within a list, people apply for a new project page which is created by the list and your workflow. Within this list i have a running project number. Now that the workflow creates the new webpage/project page, i want to let this new number appear on the new page without giving manual tasks to the project leaders. Is this possible?
    I know i could use the project title for it, but there is always a title and the project number running. Also i got a custom template running for each project page.

    • Hi DofDk
      The running number (I assume it’s the ID Field in the list Your are talking about) is not available until you save the list item. You will be able to use the number in the workflow, but it is not available to display in the form at creation. You could alternatively add a custom Field in Your list With a default calculated value that would be visible in the form – but the complexity ensuring that the same value might be a risk.
      Did this answer Your question – or did I misunderstand?
      Roger

      • Hi Roger, first of all thank you that you still observing your blog 🙂
        Well as i cannot attach any pictures in here i will try to explain in more details;

        I have a project page, within this page i have an application form (list), people do entries like title, project manager etc. After they have filled out the form, a workflow is provind a running number which i called project number (number column in the list). After they have Applied and the project number is provided by the workflow, they need to initate the workflow to create the project page.

        I used your workflow to publish the project page and it also locates with help of the project number the new website to the right URL. What i want to know is: On my project page template i got several fields, one of them is called project number. Can i get your described workflow to pre-fill this field called project number in the new webpage? I simply want to use provided contents to pre-fill the new webpage without doing it manually.

        This is the part im hanging right now, beside that i cannot get groups and uniqe permissions to run with the same workflow that creates the project page to lower manual work to a minimum.

  14. FINALLY!!!

    I’ve followed at least 10 of these with no success (for a slight different need than here but same calls etc…). Not sure if it was something here or a combination, but I was here when it WORKED! So thank you!

    I was beginning to seriously doubt this would work…

  15. First of all thank you for this greate piece of work.
    I got one problem:
    I have followed all these steps but I always get BadRequest in the responsecode. Does anybody know what can be wrong ?

    • Sigmundur, if you haven’t figured this out yet try emailing yourself the ResponseContent as well as the ResponseCode. I was getting BadRequest and it was because I had a bad parameter in my RequestHeader dictionary.

  16. hey roger,

    iam not sure if these article is watched by you at all, but imo the question from matthewmcd back in 2013 is a good one. if i go through your guide step by step, the action under point 22. brings me an error, that this variable already exists.

    Regards

  17. I’m getting an issue where most of the time this works great, but for some users it returns an InternalServerError «the URL is already in use» — even though it’s not, and the site was still created. Has anyone seen this before, and know how I can fix it? It’s making error handling a nightmare for me.

    • Hello,

      Did you fix this error? I am stucked at this point. Till now it was creating subsites properly, but suddenly it stopped.

      May be issue with template that we already created?

  18. Hi Roger
    I’m so glad that you wrote this blog. I had no mistakes when entering and also the release of the app was without fault. Only when the workflow start, there was the following Errors:

    An unhandled exception occurred during the execution of the workflow instance. Exception details: System.NotSupportedException: CannotReadAsType=Cannot read ‘Microsoft.Activities.Dynamic.DynamicObject’ as ‘System.String’ type.

    Can you tell me what I’ve overlooked?
    Best regards
    Byers

  19. Step 19: should be
    «Add string variable named type with value SP.WebInfoCreation»
    instead of
    «Add string variable named type with value SP.WebInfoCreationInformation»

  20. Hi, can I know if i want to create a document library, what should i change?? 🙂 Thank you.

  21. Tilbaketråkk: Create a SharePoint Site (SPWeb) using REST in SPD 2013 Workflow | SPUG

Legg igjen en kommentar

Fyll inn i feltene under, eller klikk på et ikon for å logge inn:

WordPress.com-logo

Du kommenterer med bruk av din WordPress.com konto. Logg ut /  Endre )

Google+-bilde

Du kommenterer med bruk av din Google+ konto. Logg ut /  Endre )

Twitter-bilde

Du kommenterer med bruk av din Twitter konto. Logg ut /  Endre )

Facebookbilde

Du kommenterer med bruk av din Facebook konto. Logg ut /  Endre )

Kobler til %s