Create a SharePoint site using REST in workflow with SharePoint Designer in office 365

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.
  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 RequestHeader2
  23. Add these variables to RequestHeader2:
NameTypeValue
UrlStringCurrentitem: TitleClick the fx button to add the Title of the current list item:
TitleStringCurrentitem: TitleClick the fx button to add the Title of the current list item:
DescriptionStringCurrentitem: TitleClick the fx button to add the Title of the current list item:
LanguageInteger1033
WebTemplateStringsts#0
UseUniquePermissionsStringfalse
__metadataDictionaryVariable: MetaDataClick the fx button to add the previous created MetaData variable:
  1. Click OK in all dialog boxes to save RequestHeader2 dictionary variable
  2. Add a new Build dictionary action
  3. Set output to new variable named RESTparameters
  4. Add a variable named parameters type Dictionarywith 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(https://sitename.com/sitecollectionname) /_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.

1 comment:

  1. Nice Article, Thank you so much giving your time for this wonderful articcle. Keep it up sharepoint development

    ReplyDelete