Create Azure WebJobs for SharePoint Online and Deploy to Microsoft Azure

Azure webjobs sharepoint 2013

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.

SharePoint deveopment training course

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 webjob 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 webjob
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

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.

Check out Best Alternative to InfoPath -> Try Now

free sharepoint training

SharePoint Online FREE Training

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

envelope
envelope

About 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"

View all posts by Bhawana Rathore →