Create Azure WebJobs for SharePoint Online and Deploy to Microsoft Azure

This SharePoint online tutorial explains, how we to create Microsoft Azure webjob for SharePoint Online using Visual Studio 2015/2017 and then how to deploy web jobs to Microsoft Azure.

For SharePoint on-premise environment we have timer jobs, by which we can do scheduled tasks in SharePoint farm. But in SharePoint Online environments we can not deploy any farm solutions but timer jobs can be created using farm solutions. But we can do scheduled tasks in SharePoint online (Download PDF for FREE) Office 365 by using Azure WebJob. Azure WebJobs are timer jobs for SharePoint online environments.

Create Azure webjob using Visual studio SharePoint Online

Here we will discuss how we can create Azure webjobs using visual studio 2015/2017 using SharePoint online environment and then we will discuss how we can deploy web jobs to Microsoft Azure.

Follow the below steps:

Open Visual Studio 2015/2017 and then click on File -> New -> Project. And then in the New Project dialog box, select a Console Application from Visual C# -> Windows category like below:

create azure webjob visual studio 2017
create azure webjob visual studio 2017

Now click on Tools -> NuGet Package Manager -> Manage NuGet Packages for Solution… like below:

create azure webjob visual studio
create azure webjob visual studio

Now in the Manage Packages for Solution, click on Browse tab and search for “App for SharePoint” and click on AppForSharePointWebToolkit. And then select the Project and click on Install like below:

create azure webjob c#
create azure webjob c#

In the Preview changes page click on OK like below:

create webjob sharepoint visual studio
create webjob sharepoint visual studio

Once the installation done successfully. The solution will look like below:

create webjob sharepoint online visual studio
create webjob sharepoint online visual studio

Now in the app.config file, we will maintain the user account and password which will be used for communicating with SharePoint Online.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<appSettings>
<add key="SPOAccount" value="******@onlysharepoint2013.onmicrosoft.com" />
<add key="SPOPassword" value="**********" />
</appSettings>
</configuration>

The app.config file looks like below:

create azure webjob visual studio 2017
create azure webjob visual studio 2017

Now in the Program.cs page we will write the code which will add an item to a SharePoint online list. The code looks like below:

Program.cs:

using Microsoft.SharePoint.Client;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Security;
using System.Text;
using System.Threading.Tasks;

namespace DemoAzureWebJobsForSharePoint
{
class Program
{
static void Main(string[] args)
{
using (ClientContext context = new ClientContext("https://onlysharepoint2013.sharepoint.com/sites/Bhawana/"))
{
context.AuthenticationMode = ClientAuthenticationMode.Default;
context.Credentials = new SharePointOnlineCredentials(GetSPOAccountName(), GetSPOSecureStringPassword());
var list = context.Web.Lists.GetByTitle("MyDemoWebjobsList");
ListItemCreationInformation newItemCreateInfo = new ListItemCreationInformation();
Microsoft.SharePoint.Client.ListItem newItem = list.AddItem(newItemCreateInfo);
newItem["Title"] = "Item added by Azure WebJobs";
newItem.Update();
context.ExecuteQuery();
}
}

private static SecureString GetSPOSecureStringPassword()
{
try
{
var secureString = new SecureString();
foreach (char c in ConfigurationManager.AppSettings["SPOPassword"])
{
secureString.AppendChar(c);
}
return secureString;
}
catch
{
throw;
}
}

private static string GetSPOAccountName()
{
try
{
return ConfigurationManager.AppSettings["SPOAccount"];
}
catch
{
throw;
}
}
}
}

Deploy WebJobs to Microsoft Azure

Now we will deploy the WebJobs to Microsoft Azure. Before procedeing further we need to create a web apps in Microsoft Azure and we need to get the Publish Profile for the web app. Follow the below articles and get the pulish profile which we will need later.

Now right click on the Project and click on “Publish as Azure WebJob…” like below:

create azure webjob
create azure webjob

In the next screen in the Add Azure WebJob, You can provide the WebJob name and also you can schedule the job according to your requirment like below. Here we have just set the run mode as “Run Continuously” like below:

create webjob sharepoint online
create webjob sharepoint online

Now in the Publish Web dialog box, click on Import like below:

create webjobs sharepoint
create webjob sharepoint

Then browse to the profile which we have imported through the above url like below:

create azure webjob visual studio
create azure webjob visual studio

Then it will populate all the details like below. You can also click on Validate Connection button to validate the connection like below.

create azure webjobs
create azure webjob

Once it will published successfully, if we will open the list, we can see few items has been added like below:

deploy webjob to azure
deploy webjob to azure

Review Web Job in SharePoint Online

We can also review the WebJob in Microsoft Azure Portal. Login to Microsoft Azure Portal and select the Web App and then select WebJobs and then you can see WebJob running like below:

deploy web job in azure portal
deploy web job in azure portal

Schedule Azure WebJobs in Visual Studio for SharePoint Online

Now, we will see how to schedule Microsoft Azure WebJobs in Visual Studio for SharePoint Online.

Once we deploy the WebJob to Microsoft Azure from Visual Studio 2015, next time when we will try to deploy from visual studio 2015 like below.

schedule webjobs in sharepoint online

We will see directly the below dialog box.

schedule azure webjobs in sharepoint online

We will not see the scheduler dialog box.

schedule azure webjobs in sharepoint online

To modify the scheduling, expand the properties in the solution and open the “webjob-publish-settings.json” file like below:

schedule webjobs in sharepoint

Then you can change the schedule and deploy the Web App.

schedule webjobs in sharepoint

You can also follow another approach. Delete the “webjob-publish-settings.json” file from the solution and then try publishing the WebApp through Right-click on the Project -> Publish as Azure WebJo…

Now you will be able to see the Scheduler window.

Deploy WebJob to Microsoft Azure using Upload zip file method in SharePoint Online

Now, we will see, how to deploy a web job to Microsoft Azure using upload zip file method in SharePoint Online Office 365.

Follow the below steps to deploy WebJob to Microsoft Azure using the Upload zip file method in SharePoint Online.

Open the Visual Studio 2015 solution and then Right-click on the project and click on Build.

Then open the Bin -> Release/Debug folder. Here my folder path is like below:

E:\Users\Administrator\Documents\Visual Studio 2015\Projects\DemoAzureWebJobsForSharePoint\DemoAzureWebJobsForSharePoint\bin\Release

Select all the files and make a zip folder. Here I have created a zip file as DemoAzureWebJobsForSharePoint.zip.

Now Open the Microsoft Azure Portal and then click on All resources like below:

SharePoint online deploy WebJob to Microsoft Azure

Then click on the particular web app into which you want to deploy the WebJob. Then click on WebJobs and then click on the +Add button like below:

deploy WebJob to Microsoft Azure SharePoint Online

Once you click on the +Add button, it will open the Add WebJob screen. Give a name for the WebJob, choose the .zip file which we have created above and then choose the Type (schedule) and Scale like below:

deploy WebJob to Microsoft Azure

Now once it is created successfully, you can see the web job like below. If it did not appear then click on the Refresh button.

deploy WebJob to Microsoft Azure using Upload zip file method

The job will run continuously and if you will open the SharePoint online list, you can see it will add items to the SharePoint list.

You may like following SharePoint tutorials:

Hope this will be helpful to create web jobs in SharePoint online and also to how to deploy web jobs to Microsoft Azure.

free sharepoint training

SharePoint Online FREE Training

JOIN a FREE SharePoint Video Course (3 Part Video Series)

envelope
envelope

Bhawana Rathore

Hello Everyone!! I am Bhawana a SharePoint MVP and having about 8+ years of SharePoint experience as well as in .Net technologies . I have worked in all the versions of SharePoint from wss to Office 365. I have good exposure in Customization and Migration using Nintex, Metalogix tools . Now exploring more in SharePoint 2016 :) Hope here I can contribute and share my knowledge to the fullest. As I believe "There is no wealth like knowledge, and no poverty like ignorance"

>