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

  1. Open Visual Studio 2013.
  2. Click File=> New => Project.
  3. Select Console Application template, enter name and click OK button.

    Console

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

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

    App for SharePoint Web Toolkit

  6. In the License Acceptance, click I Accept button.

     License Acceptance

  7. Installing the package is shown below:

    Installing

  8. Package installed successfully. Click Close button.

    Package installed successfully

  9. Make sure the below highlighted CS files are added.

    highlighted CS

  10. 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.
    1. <?xml version="1.0" encoding="utf-8" ?>  
    2. <configuration>  
    3.     <startup>  
    4.         <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup>  
    5.     <appSettings>  
    6.         <add key="SPOAccount" value="[email protected]" />  
    7.         <add key="SPOPassword" value="@Macro9876" /> </appSettings>  
    8. </configuration>  
  11. 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.
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Configuration;  
    4. using System.Linq;  
    5. using System.Text;  
    6. using System.Security;  
    7. using System.Threading.Tasks;  
    8. using Microsoft.SharePoint.Client;  
    9. namespace AzureWebJob  
    10. {  
    11.     class Program  
    12.     {  
    13.         static void Main(string[] args) {  
    14.             try {  
    15.                 using(ClientContext context = new ClientContext("https://c986.sharepoint.com")) {  
    16.                     // Use default authentication mode  
    17.                     context.AuthenticationMode = ClientAuthenticationMode.Default;  
    18.                     // Specify the credentials for the account that will execute the request  
    19.                     context.Credentials = new SharePointOnlineCredentials(GetSPOAccountName(), GetSPOSecureStringPassword());  
    20.                     // Create List Items  
    21.                     var oList = context.Web.Lists.GetByTitle("Azure Web Job List");  
    22.                     ListItemCreationInformation newItemCreateInfo = new ListItemCreationInformation();  
    23.                     Microsoft.SharePoint.Client.ListItem newItem = oList.AddItem(newItemCreateInfo);  
    24.                     newItem["Title"] = "Item added by Azure Web Job at " + DateTime.Now;  
    25.                     newItem.Update();  
    26.                     context.ExecuteQuery();  
    27.                     Console.WriteLine("Azure Web Job: Successfully completed.");  
    28.                 }  
    29.             } catch (Exception ex) {  
    30.                 Console.WriteLine(ex.Message);  
    31.                 Console.WriteLine(ex.StackTrace);  
    32.             }  
    33.         }  
    34.         private static SecureString GetSPOSecureStringPassword() {  
    35.             try {  
    36.                 Console.WriteLine(" --> Entered GetSPOSecureStringPassword()");  
    37.                 var secureString = new SecureString();  
    38.                 foreach(char c in ConfigurationManager.AppSettings["SPOPassword"]) {  
    39.                     secureString.AppendChar(c);  
    40.                 }  
    41.                 Console.WriteLine(" --> Constructed the secure password");  
    42.                 return secureString;  
    43.             } catch {  
    44.                 throw;  
    45.             }  
    46.         }  
    47.         private static string GetSPOAccountName() {  
    48.             try {  
    49.                 Console.WriteLine(" --> Entered GetSPOAccountName()");  
    50.                 return ConfigurationManager.AppSettings["SPOAccount"];  
    51.             } catch {  
    52.                 throw;  
    53.             }  
    54.         }  
    55.     }  
    56. }  

Publish directly as Azure WebJob from Visual Studio:

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

    Publish

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

    WebJob run mode

  3. Visual Studio installs WebJobs publishing NuGet Package.

    installs WebJobs publishing

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

    configuration

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

    publish

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

    New

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

    Create

  8. Creating a site is in progress.

     site in progress

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

    Solution Explorer

    Publish

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

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

     WEB APPS

  3. Click WEBJOBS.

    WEBJOBS

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

    RUN

  5. Job ran successfully, click Logs to review.

    Logs

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

    WebJob details

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

    details
  8. 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).

    Azure Web Job List

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

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

    App Services

  3. In the Settings section, click WebJobs.

    Settings

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

    webjob

Summary

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

Up Next
    Ebook Download
    View all
    Learn
    View all