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 Tutorial Contents
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:
Now click on Tools -> NuGet Package Manager -> Manage NuGet Packages for Solution… like below:
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:
In the Preview changes page click on OK like below:
Once the installation done successfully. The solution will look like below:
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:
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:
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:
Now in the Publish Web dialog box, click on Import like below:
Then browse to the profile which we have imported through the above url like below:
Then it will populate all the details like below. You can also click on Validate Connection button to validate the connection like below.
Once it will published successfully, if we will open the list, we can see few items has been added like below:
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:
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.
We will see directly the below dialog box.
We will not see the scheduler dialog box.
To modify the scheduling, expand the properties in the solution and open the “webjob-publish-settings.json” file like below:
Then you can change the schedule and deploy the Web App.
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.
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:
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:
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:
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.
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:
- Create a Web API for SharePoint Online and Deploy to Microsoft Azure using Visual Studio (Step by Step tutorial)
- Call Web API from SharePoint Online using httpsend workflow activity
- Create Highcharts in SharePoint using Rest API
- SharePoint Hosted Add-in: Send Email to SharePoint group using visual studio workflow
- SharePoint Hosted Add-in: Send Email using visual studio workflow
- How to send email from SharePoint Online using Rest API?
- Create a Leave Request Approval Workflow using SharePoint Designer 2013
- Steps to create deploy and Associate reusable workflow using SharePoint designer 2013
- Develop SharePoint workflows using visual studio as SharePoint hosted App
- How to Create a QnA Bot in Azure
Hope this will be helpful to create web jobs in SharePoint online and also to how to deploy web jobs to Microsoft Azure.
Bhawana Rathore is a Microsoft MVP (3 times in Office Apps & Services) and a passionate SharePoint Consultant, having around 10 years of IT experience in the industry, as well as in .Net technologies. She likes to share her technical expertise in EnjoySharePoint.com and SPGuides.com