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
- Visual Studio 2013 or the latest versions.
- Office 365 Site. If you don’t have an Office 365 site, please try the trial version.
- 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 the Console Application template, enter your name, and click the 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 the I Accept button.

- Installing the package is shown below.

- The package was installed successfully. Click the 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. 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> - 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; } } } }





















Vinay AyinapurapuPosted Apr 15, 2021, 9:39 PM
Any idea how to modify this for MFA ed account? Instead user name and password, can we use app ID or App secret or securiy principal?
Vasanth Kumar TPosted Mar 21, 2017, 8:54 AM
@Vijai Wow Awesome article.. Thank you so much... But is it possible to run this web Job without having Azure Subscriptions... Kindly reply me ASAP... Regards
Tarun DPosted Aug 4, 2016, 7:26 AM
Nice article. Thanks for sharing..
Prasanna MuraliPosted Jul 29, 2016, 11:37 AM
Nice one..
Vignesh ManiPosted Jul 29, 2016, 8:46 AM
Good one