Dynamics CRM and Azure Scheduler – The final pieces of the puzzle

Lately it has been quite hectic at the office, project deadlines were shifting, functionality was added and project issues had to be resolved. Not the most optimal situation to write blog articles.
Fortunately, things calmed down. Time to pick up this quest again.

Puzzle

In the previous article I focussed on the Azure webjob. It turns out that the basic techniques required to write web jobs are pretty straight forward. It costed me a little while, but now I did solve the puzzle. Time for an update…

WebJobs

Azure WebJobs is one of the most cool features in the Azure stack. Basically you can throw anything at it, and instruct to run as a webjob.
In my case I wrote a simple windows console application in which I’m able to receive command line arguments (parameters).
In case there are parameters, these are written to the console. Not particulary rocket science.

Awj1

If you want to go easy you can publish the project from within Visual Studio to Azure (in that case you need to have the Azure SDK installed, and you need to choose Azure WebJob as the project type).
What the Visual Studio wizard does for you is create an Azure WebApp. Within the Azure WebApp you can add any Azure Webjob you want.

In case you want to do it manually, you need to create the Azure WebApp by hand and create a zip file containing the entire contents of the Bin\Debug or Bin\Release folder in a zip file. Thus all dll’s, config files, resources etc.

Awj2

Once done, you can create in the Azure Management Portal a new WebJob using the zip file you just created. I’ll go for the manual route….

Within the Azure WebApp created (“myazurewebjobs”), I go to the WebJobs tab and click on the link “Add a new Job”. The following popup appears.

Awj4

I enter the name, choose the zip file I created before and specify that the WebJob has to run on demand. Remember I want to use it from within the Azure Job Scheduler.
I click the apply button and the job is created and executed.

This was the simple part!

Now we have come to this point we need to do three more things:

  1. pass parameters to the newly created webjob.
  2. determine what the credentials are and what url to call in order to run the webjob.
  3. call the webjob using parameters from within the Azure Job Scheduler.

Some googling learned me that passing parameters to a WebJob is simple. The WebJob is activated by calling an URL. In order to pass parameters, the URL needs to be enriched with the argument:

?arguments=arg1 arg2 argN

Now the tricky part, determine the Url.

The basic url syntax is:

https://{userName}:{password}@{WebAppName}.scm.azurewebsites.net/api/triggeredwebjobs/{WebJobName}/run

behind the run we have to append the argument containing the arguments for the WebJob.

https://{userName}:{password}@{WebAppName}.scm.azurewebsites.net/api/triggeredwebjobs/{WebJobName}/run?arguments=arg1 arg2 argN

The username, password, webappname and webjobname can be retrieved from the AzurePortal, by going to the dashboard of our Azure WebApp and click the link “Download the publish profile”.

Awj5

Once downloaded open the file and get the username, password. The webAppName and WebJobName can be retrieved from the portal. In my case the WebAppName = “myazurewebjobs” and my WebJobName = “MyFirstWebJob”

Assembled the string might look like this (this is just an example):

https://$TestAzureWebJob1:XqnZaghFedb2urgqfWntf6ZZZSWw6QN8EqpyqmPfznPNxx2Nqf7uY3pjAEftB@testazurewebjob1.scm.azurewebsites.net/api/triggeredwebjobs/TestAzureWebJob/run?arguments=This is sooo cool!

Having the string assembled, it is time to go to the Azure Job Scheduler

Azure Job Scheduler

In the Azure Job Scheduler (in my case “Scheduler Demo”) I go to the Jobs tab, and click the Add (“+”) button in the bottom of the screen. I pick the option to add a new job to the Job Scheduler.

Awj6

In the first strap I have to specify the Azure region and the Job Collection I want to add the job to. Then I go to the next step, which is the most interesting one.

Awj7

In order to execute the Azure WebJob we have to do a HTTP POST using the URL (with parameters) we constructed before. Once entered I go the final step.

Awj8

In the schedule step, I choose for a One-time job, starting Now. This will mimic the situation we will face when using the scheduler from CRM.

Once done, we can verify on the history tab if the Scheduler job executed succesfully.

Awj9

Finally we can go to the web job we created before to check if it did run.

Awj10

As you can see, the parameters passed on the URL are coming in as seperate parameters. Which means we have full control over the situation.

Wow, what have we done?

Schedule1

Right now we have implemented the logic for the Azure Job Scheduler, we have implemented an Azure WebJob and we hooked up the Azure Scheduler to the Azure WebJob.

What remains is hooking up Dynamics CRM. That part will be covered in the next article.

I hope you enjoy the ride, till so far!

2 thoughts on “Dynamics CRM and Azure Scheduler – The final pieces of the puzzle

  1. […] Dynamics CRM and Azure Scheduler – The final pieces of the puzzle […]

  2. […] Dynamics CRM and Azure Scheduler – The final pieces of the puzzle […]

Leave a Reply

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