One of the most common requests received by a SharePoint admin or content owner is to create a new site for a staff member. Automating this process has been covered in detail already, and others have extended the solution to accommodate custom site templates. In the process of implementing this solution however, I found that one of the major issues we encounter is when a site already exists and the requester just does not know about it. For governance purposes it would be great to have some additional automation to check if the site already exists, and avoid even running the site creation workflow. Basically a Try-Catch block SharePoint Designer.
Luckily, this is not too difficult to do, and can be added quite easily into the existing site creation workflows noted earlier. The only additional logic needed is some extra dictionaries, variables, and an REST call to find out if the URL returns anything.
This will start off assuming you’ve already created a workflow which creates a new site. The links we mentioned earlier point to a couple of solid starting points for that work:
- CREATE A SHAREPOINT SITE (SPWEB) USING REST IN SPD 2013 WORKFLOW
- Create Site From Template using SharePoint 2013 Workflow
Much like many SharePoint administration tasks, there may be a cleaner way to accomplish this goal but this routine works well and is easy to implement.
Create a new dictionary to hold the requested site’s URL. This will be passed into the HTTP web service call.
Url | String | Current Item:Site URL (this is the value the requester entered in the New Item form)
Create another dictionary to store the result of the HTTP web service call. The results must be returned as a dictionary. Note: This may not be 100% necessary, but I prefer to initialize my variables. One of the holdovers from my Computer Science days.
Url | String | <blank> (this will be blank)
Add an action to call HTTP web service.
- Address: https://<SharePoint Parent Site/[%Current Item:Site URL%]/_api/web/URL
- Request Type: HTTP GET
- RequestHeaders: This is the same dictionary used in the site creation call. For reference [Accept | String | application/json;odata=verbose; Content-Type | String | application/json;odata=verbose]
- RequestContent: The dictionary we created in Step 1
- ResponseContent: The dictionary we created in Step 2
- Everything else can be left blank
Here we’re calling the REST Web Resource’s URL property. This will eventually be returned as either blank if the site doesn’t exist, or with the URL if it does. Obviously we don’t really need the URL, as we already know it, but the fact that it was returned is what we’re using to confirm that it exists.
Add an action to Get and item from a Dictionary. We need to extract the URL value from the Json response that was returned to the JsonRequestGetUrl variable.
Make sure to put “d/Url” as the value from the dictionary we want to get. JsonResultGetUrl is the dictionary we want to retrieve it from, and we are putting that into a String variable which for this example is called VarUrlFromDictionary.
Now the easy part. Make a string variable with the full URL of the requested site, and compare it to the URL that was returned and extracted in Step 4. My variable is called CheckExistingUrl, and it’s just a string with the full URL of the requested site.
I first checked if they did not match, and if so, I went ahead and created the site using the HTTP web service call. If the variables did match, I changed the Workflow status to say that the site already exists, and provided a link to the existing site.
There are definitely more pieces which can be added on to this, such as an approval process, a separate column with the links, email notifications, etc. Hopefully this quick tutorial will be helpful in automating your site creation even more. Looking for more SharePoint guidance? Reach out to New Signature today to see how we can help upgrade, improve and evangelize your SharePoint sites.