Azure WebJobs for Office 365 SharePoint Online

As part of the on-premises customizations, we develop SharePoint timer jobs for any operations which needs to be done on a regular interval. The Timer jobs are packaged as part of our full trust solutions. In Office 365 SharePoint Online full trust code are not supported, and there is no option for us to deploy our full trust custom solutions. So there is a need to look for alternate for these timer jobs which makes use of CSOM/REST client site APIs for accessing the SharePoint resources.
As of now we have two options,
  1. Azure WebJobs 
  2. On-Premises Windows scheduled tasks 

 In this post, we will look into the Azure WebJobs option for performing any Office 365 SharePoint Online operations. For this particular case I'll be using PowerShell CSOM.
Use case: Add a new item to SharePoint custom list in Office 365 site on a regular interval. 


It's a very simple PowerShell CSOM used to create a list item, and its writes the current time as value to the title field. For simplicity I'll be creating user context based on the passed username and password. You can also create the addin-only (App-only) context by using the client ID and client secret. As said earlier, trying to keep it simple here.
Key things to note here, we are using the SharePoint Client SDK in our script. We need to make sure that to reference it from the local folder and also while packaging we need to package the PowerShell script and referenced dlls in the same folder. One more thing, we will be using "Write-Output" cmdlet in order to write anything back, "Write-Host" won't work here. 

Download: Complete zip file can be downloaded from here

Create Azure Web App

Follow the steps detailed in the video, to the create the Azure Web App, and follow the below steps for create WebJob. Open your WebApp from the Azure dashboard and in the settings, select the WebJobs. In the WebJobs blade, click on the add icon to create a new one. Give a name to your WebJob and in the "How to Run" drop down, select "continuos" for any scheduled job and "on demand" for running the job on demand. You can determine whether you want to run multiple instance in your app service or single instance. Now its the time to upload our package. Zip the folder where we have kept our PowerShell script and referenced dlls. Upload that zip file in the file upload section of the WebJob and hit create. Voila we have successfully create our WebJob. Now its the time to test the job, right click on our job from the list of WebJobs for the WebApp and click Run.

Once you hit the run, you should be able to see the status of the job status to "Running" and changes to "Completed". We can access the logs by clicking on the logs url.

In the WebJobs logs, you'll be able to see the system status like "Initializing", "Running" and finally to "Success".
Along with it there is also the information which wrote about the status of the script using the "Write-Output" cmdlet. By this way we can log any script exceptions also. In this script, shows the information about connecting the SharePoint Web and successfully creating an item inside the list.

Lets log in to the SharePoint site, mentioned in the script You can see the list item, successfully created in the list with the timestamp as its title.

While creating the WebJob we manually uploaded the package, let's also know how we do some continuos build and deployment. For the Azure WebApp, we have several deployment options like publishing using the Visual Studio publishing profile, which you can directly download from the WebApp settings and use. We also have the FTP portal access to connect by using the deployment credentials. I'm a big fan of Git, so I'm going to use Git as the deployment source.

Lets go back to the WebApp in the Azure portal and in the settings blade under the "Publishing" section. Select the deployment source and in the deployment source options select the local git repository which provisions the repository for this WebApp. Go to the deployment credentials section and create a username and password. Once the deployment source is created, you should have your git repository url, clone the repo and start pushing the changes using the credentials we created earlier. Once the repo is cloned locally, the folder structure should like below.

That's how we create WebJob in Azure WebApp and configure the job to connect to SharePoint and perform operations over the SharePoint resources using CSOM or REST.

Happy coding ! Peace !!


Post a Comment