Steps to create Azure WebJobs for SharePoint online and deploy in Microsoft Azure

Azure webjobs sharepoint 2013

This SharePoint online tutorial explain how we can create Microsoft Azure webjobs for SharePoint online using Visual Studio 2015 and then how to deploy web jobs to Microsoft Azure. For SharePoint onpremise 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.


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

WebJobs Development for SharePoint Online

Open Visual Studio 2015 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:


Azure webjobs sharepoint
Azure webjobs sharepoint

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

Azure webjobs sharepoint online
Azure webjobs sharepoint online

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:

Azure webjobs sharepoint 2013
Azure webjobs sharepoint 2013

In the Preview changes page click on OK like below:


Azure webjobs sharepoint 2013
Azure webjobs sharepoint 2013

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

Azure webjobs sharepoint 2016
Azure webjobs sharepoint 2016

Now in the app.config file, we will maintain the user account and password which will be used for comuunicating 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:

 Steps to create Azure WebJobs for SharePoint online and deploy in Microsoft Azure
Steps to create Azure WebJobs for SharePoint online and deploy in Microsoft Azure

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.

Steps to create a web site in Microsoft Azure for SharePoint Online

How to get publish profile for Microsoft Azure web site?

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

create microsoft azure webjobs sharepoint online
create microsoft azure webjobs sharepoint online

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 microsoft azure webjobs sharepoint online using visual studio
create microsoft azure webjobs sharepoint online using visual studio

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

deploy azure webjobs sharepoint online
deploy azure webjobs sharepoint online

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



create microsoft azure webjobs sharepoint online using visual studio 2015
create microsoft azure webjobs sharepoint online using visual studio 2015

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

deploy webjobs to microsoft azure
deploy webjobs to microsoft azure

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

deploy azure webjobs sharepoint online to microsoft azure
deploy azure webjobs sharepoint online to microsoft azure

Review Web Job in SharePoint Oline

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 azure webjobs sharepoint online to microsoft azure
deploy azure webjobs sharepoint online to microsoft azure

Read some sharepoint online 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

You May Also like the Following SharePoint Online Tutorials:

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 →