Using Azure Logic Apps for Site Provisioning in Office 365 part 1

By | 2015-07-16

This post will describe a very simple flow for the creation of Site Collections inside a SharePoint Online tenant. I wanted to use some of the new Azure Apps that are available, in particular Azure WebJobs and Azure Logic Apps. I already wrote about the Logic Apps earlier.

The scenario/flow is fairly easy:

  1. User adds a List Item in a SharePoint List named Site Requests
  2. When this List Item is Approved (yes/no field 🙂 ) a Flow is started
  3. The Flow will
    1. Create a Site Collection
    2. Send a notification Email

In this post I am going to describe the part that will actually create a Site Collection in SharePoint Online. This is not an enterprise ready code implementation of it, just a sample to show it can be done.

This is how I wanted this part of the overall process to go:


A JSONObject will be send on a Azure Service Bus Queue.

   Title: "Site Collection Title", 
   TemplateCode: "STS#0"

An Azure WebJob will process Messages on this Service Bus and Create a SharePoint Site Collection.

First we create a Service Bus in Azure (via PowerShell).

New-AzureSBNamespace -Name "siterequests-ns" -Location "Your Region" -CreateACSNamespace $true -NamespaceType Messaging

Within this Service Bus we create a Queue:



For our WebJob to be able to read messages that are sent on the queue we need a Connection String. Go to the Configuration Page of the Queue. There you add a Shared Access Policy named WebJob. Provide Manage rights and Save:

azure-servicebus-siterequests-accesspolicy(Here I already added another Policy already I will need later.)

On the Dashboard Page of you Queue, you can see what the Connection String is by clicking the “View Connection String” button on the Right side of the Page. It will look like this:

Endpoint=sb://;SharedAccessKeyName=WebJob;SharedAccessKey=SOME KEY

Now we use the Azure WebJobs SDK and Visual Studio (2013) to create a WebJob Project. You will need the Connection String we just found and add it to the App.Config (for local testing)

    <add name="AzureWebJobsServiceBus" 
         connectionString="Endpoint=sb://;SharedAccessKeyName=SiteProvisioningWebJob;SharedAccessKey=SOME KEY" />

The main of your WebJob looks like this now:

static void Main()
  var host = new JobHost();
  // The following code ensures that the WebJob will be running continuously

And we have Functions class, we will add our ProcessSiteRequests method which needs the following attributes to work with our Queue:

public static void ProcessSiteRequest(
            [ServiceBusTrigger("siterequests")] String message,
            TextWriter log)
  log.WriteLine("ServiceBusTrigger for JSON SiteRequests");
  try {
    // parse incoming json string to .Net CLR object (POCO)             
    SiteRequest request = SiteRequest.FromJSON(message);
    if (request != null) {
      // come up with an URL for this Site Collection (TEST CODE ONLY)
      string siteUrl = TenantAdmin.GetSiteUrl(request.Title);
      // use this object to actually create the Site Collection in SPO
      TenantAdmin.CreateSPOSite( request.Title,
    else {
      log.WriteLine("Unable to read a SiteRequest from queue.");
  catch (Exception exc) {
    throw exc;

I am using Newtonsoft.Json to translate to and from Json strings to objects.

public class SiteRequest {
    public string Title { get; set; }
    public string LanguageCode { get; set; }
    public string TemplateCode { get; set; }

    public string ToJson() {
        return SiteRequest.ToJson(this);

    public static string ToJson(SiteRequest request) {
        if (request != null)
            return JsonConvert.SerializeObject(request);
            return string.Empty;

    internal static SiteRequest FromJSON(string queueMessage) {
        SiteRequest result = null;
        if (!string.IsNullOrEmpty(queueMessage)) {
            result = JsonConvert.DeserializeObject<SiteRequest>(queueMessage);
        return result;

The TenantAdmin class just contains some CSOM and a SiteCreationProperties object to actually create a Site Collection. I left this out here.

Anyway now we deploy our WebJob to Azure. Once that is completed, we need to add the Connection String to our deployed WebJob too. Now we use the new (preview) Azure Portal to navigate to our newly deployed WebApp with our new WebJob. We use the Application Settings for the WebApp to add the same connection string we added to our App.config:


By the way, as part of any WebJob deployment, and per instruction of the Azure WebJob SDK, you will need to add the [AzureWebJobsDashboard] and [AzureWebJobsStorage] keys too, but you can find the howto for those in the SDK.

We wrote some code, we configured some Azure, now let us test this part of the overall solution.

You can easily create a Console Application in VS2013, add the Azure Service Bus SDK, make sure you add the Connection String again and you can send a simple JSON object on the Queue with this code:

QueueClient queueClient = QueueClient.Create("siterequests");

Console.WriteLine("\nSending message to Queue...");
// json formatted message
string jsonmessage = "{ Title:'TEST - A Site Collection Title',TemplateCode:'STS#0'}";

// message holder
BrokeredMessage bm = new BrokeredMessage(jsonmessage);
// send it on the queue

If you have done it correctly, the message will be sent on the Queue.

And your WebJob will receive the JSON Object and start creating your first Site Collection.

You van monitor your Azure Queue and see a message being submitted.

You can monitor your Azure WebJob and see the method call actually executing.

For this, open the WebJobs Settings in the new Azure Portal, and click on the LOGS link. You will be presented with an overview of the calls that were made and other details:


Pro Tip when working with Azure Service Bus (and Queues): Get this tool: Service Bus Explorer

Next time, we create a Logic App that will use this Queue and WebJob. Stay tuned.

29 thoughts on “Using Azure Logic Apps for Site Provisioning in Office 365 part 1

  1. Pingback: Using Azure Logic Apps for Site Provisioning in Office 365 part 2 | Stef van Hooijdonk

  2. Tomasz Foltman

    FYI: Look like plenty of links and images are not working on this and other pages…

    1. stefvanhooijdonk Post author

      I know, had to migrate a couple of times, lost the images 🙁


Leave a Reply to Tomasz Foltman Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.