How to Create Azure WebJob For SharePoint 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 the latest versions.
  2. Office 365 Site. If you don’t have an Office 365 site, please try the 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 the Console Application template, enter your name, and click the OK button.
    Console Application
  4. In Solution Explorer, right-click References and click Manage NuGet Packages.
    NuGet Packages
  5. In Manage NuGet Packages, search for “App for SharePoint Web Toolkit” and click Install.
    Toolkit
  6. In the License Acceptance, click the I Accept button.
    License Acceptance
  7. Installing the package is shown below.
    Package
  8. The package was installed successfully. Click the Close button.
    Package installed successfully
  9. Make sure the below highlighted CS files are added.
     CS files
  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. The alternate option can be using OAuth and including 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="[email protected]" />
            <add key="SPOPassword" value="@Macro9876" />
        </appSettings>
    </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 that 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.

  1. In the Solution Explorer, right-click Publish as Azure WebJob.
    Solution Explorer
  2. Select WebJob run mode as Run on Demand. Click the OK button.
    WebJob run mode
  3. Visual Studio installs WebJobs publishing the NuGet Package.
    Visual Studio installs
  4. This adds the below-mentioned file, containing the configuration for the Azure WebJob.
     Azure WebJob
  5. Select Microsoft Azure Websites as a target to publish and click the Publish button.
    Microsoft Azure Websites
  6. Enter the credentials for the Azure portal and click the New button to create a new Website in the Azure portal.
    New Azure portal
  7. Enter the details for creating a new site and click the Create button.
    Create button
  8. Creating a site is in progress.
    Progress
  9. In the Solution Explorer, right-click Publish. Click the Publish button.
    Publish button
    Publish web

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.
    Left navigation
  3. Click WEBJOBS.
    WEBJOBS
  4. You can see the Web job which we have created. Select the job and click Run Once.
    Web jobsite
  5. The job ran successfully, click Logs to review.
    Logs to review
  6. You can see WebJob details and the recent job runs.
    WebJob details
  7. Click the last run job and you can see the Web job run details.
    Web job run details
  8. Navigate to the SharePoint list “Azure Web Job List” and you can see a new item added successfully with the last modification by, as Vijai Anand (the account, that I used to execute the code).
    Azure Web Job List

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

  1. Navigate to Azure Portal.
  2. In the left navigation, click App Services and click the Website that 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.
    Web job run

Summary

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


Similar Articles