How to Create Azure WebJob For SharePoint 2013 Online

Introduction

In SharePoint 2010/2013 On Premise, we have Timer Jobs, which perform repetitive, scheduled tasks. For example, you may need a timer job to fetch Sharepoint list items and send them as reports on a daily basis. This timer job can be created as a farm solution and can be deployed in SharePoint 2010/2013 On Premise Server, whereas in SharePoint Online, you are not allowed to deploy the farm solutions. To overcome this, you can create Azure WebJobs, which act as scheduled timer jobs for SharePoint Online.

In this article, you will learn how to create Azure WebJobs for SharePoint 2013 Online.

Prerequisites

  1. Visual Studio 2013 or latest versions.

  2. Office 365 Site. If you don’t have Office 365 site, please try trial version.

  3. Access to Azure Portal. If you don’t have an Azure account, please try the  trial version.


Create your Console Application

  • Open Visual Studio 2013.

  • Click File=> New => Project.

  • Select Console Application template, enter name and click OK button.











  • In Solution Explorer, right click References and click Manage NuGet Packages.












  • In Manage NuGet Packages, search for “App for SharePoint Web Toolkit” and click Install.


  • In the License Acceptance, click I Accept button.
















  • Installing the package is shown below:












  • Package installed successfully. Click Close button.


  • Make sure the below highlighted CS files are added.














  • Open App.Config and replace the file with XML, given below, where you can specify the credentials, which allows you to execute CSOM code on your sites. Note: I am using my credentials in this article to execute the code but the best practice can be to create a service account and use it. Alternate option can be using OAuth and include authentication tokens in your requests to avoid specifying the account/password. Please refer to this article.

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

<startup>

<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />

</startup>

<appSettings>

<add key="SPOAccount" value="vijaianand@c986.onmicrosoft.com" />

<add key="SPOPassword" value="@Macro9876" />

</appSettings>

</configuration>

  • Open Program.cs and replace the file with the code, given below. Note: When you execute/run the Azure WebJob, it will create an item in “Azure Web Job List” and the modified by will be the account name which you have specified in the App.Config.

using System;  

using System.Collections.Generic;  

using System.Configuration;  

using System.Linq;  

using System.Text;  

using System.Security;  

using System.Threading.Tasks;  

using Microsoft.SharePoint.Client;  

namespace AzureWebJob  

{  

class Program      

{  

static void Main(string[] args) {  

try 

{  

using(ClientContext context = new ClientContext("https://c986.sharepoint.com")) 

{  

// Use default authentication mode                     

context.AuthenticationMode = ClientAuthenticationMode.Default;   // Specify the credentials for the account that will execute the request                     

context.Credentials = new SharePointOnlineCredentials(GetSPOAccountName(), GetSPOSecureStringPassword());  

// Create List Items                    

 var oList = context.Web.Lists.GetByTitle("Azure Web Job List");                      

ListItemCreationInformation newItemCreateInfo = new ListItemCreationInformation();                      

Microsoft.SharePoint.Client.ListItem newItem = oList.AddItem(newItemCreateInfo);                      

newItem["Title"] = "Item added by Azure Web Job at " + DateTime.Now;                      

newItem.Update();                      

context.ExecuteQuery();                      

Console.WriteLine("Azure Web Job: Successfully completed.");                 

 }              } 

catch (Exception ex) 

{                  

Console.WriteLine(ex.Message);                  

Console.WriteLine(ex.StackTrace);              

}          }  

private static SecureString GetSPOSecureStringPassword() {  

try 

{                  

Console.WriteLine(" --> Entered GetSPOSecureStringPassword()");                 

 var secureString = new SecureString();   foreach(char c in ConfigurationManager.AppSettings["SPOPassword"]) 

{                      

secureString.AppendChar(c);                  

}                  

Console.WriteLine(" --> Constructed the secure password");  

return secureString;              

catch 

{  

throw;              

}          }  

private static string GetSPOAccountName() 

{  

try 

{                  

Console.WriteLine(" --> Entered GetSPOAccountName()");   return ConfigurationManager.AppSettings["SPOAccount"];              

catch 

{  

throw;              

}          }      

}  }  


Publish directly as Azure WebJob from Visual Studio:

  • In the Solution Explorer, right click Publish as Azure WebJob.













  • Select WebJob run mode as Run on Demand. Click OK button


  • Visual Studio installs WebJobs publishing NuGet Package.








  • This adds the below mentioned file, containing the configuration for the Azure WebJob.


  • Select Microsoft Azure Websites as a target to publish and click Publish button.












  • Enter the credentials for Azure portal and click New button to create a new Website in Azure portal.









  • Enter the details for creating new site and click Create button.











  • Creating a site is in progress.












  • In the Solution Explorer, right click Publish. Click Publish button.


























Run/Monitor the Job and Review Logs in Azure Old Portal

  • Navigate to Azure Portal. In the left navigation, click Web apps. Click AzureWebJobSite web app.


  • Click WEBJOBS.







  • You can see the Web job which we have created. Select the job and click Run Once.















  • Job ran successfully, click Logs to review.


  • You can see WebJob details and the recent job runs.








  • Click last run job and you can see the Web job run details.









  • Navigate to SharePoint list “Azure Web Job List” and you can see a new item added successfully with the last modified by, as Vijai Anand (the account, which I used to execute the code).


Run/Monitor the Job and Review Logs in Azure New Portal

  • Navigate to Azure Portal. In the left navigation, click App Services and click the Website which we have created.


  • In the Settings section, click WebJobs.


  • Select the Webjob and click Run. Once the job is completed, click Logs to see the job run details.


Summary

Thus in this article, you have seen, how to create Azure WebJob for SharePoint 2013 Online.

Recent Posts

See All