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 latest versions.
Office 365 Site. If you donāt have Office 365 site, please try 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 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.
Comments