Share via


Azure: Deploying WebJobs for SharePoint online

Introduction:

As a part of this article, we will discuss Azure webjobs, how we can create and deploy an Azure webjobs in Microsoft Azure to use in SharePoint Online Office 365 site. Azure webjobs are like timer jobs for SharePoint Online Office 365 sites. In SharePoint on-premise environment we usually create and user timer jobs, by which we can do scheduled tasks in SharePoint farm. But in SharePoint Online environments we can not deploy any farm solutions but timer jobs can be created using farm solutions. So we can use Azure WebJobs to schedule tasks in SharePoint Online Office 365. Since we can not deploy farm solutions in Office 365, we have to use webjobs to schedule a task.

Steps to Create Azure WebJobs:

Follow below steps to create Azure webjobs using visual studio 2015 using SharePoint online environment:

WebJobs Development:

Open Visual Studio 2015 and then click on File -> New -> Project. And then in the New Project dialog box, select a Console Application from Visual C# -> Windows category. Make sure .NET Framework 4.5 version has been chosen.

Once the console application is ready click on Tools -> NuGet Package Manager -> Manage NuGet Packages for Solution... And then in the Manage Packages for Solution, click on Browse tab and search for "App for SharePoint" and click on AppForSharePointWebToolkit. And then select the Project and click on Install like below:

https://www.enjoysharepoint.com/wp-content/uploads/2018/07/Webjobs-sharepoint-2013.png

Then in the Preview changes page click on OK. This will start the installation and once the installation done successfully, two files will be added to solution as: SharePointContext.cs and TokenHelper.cs.

Now in the app.config file, we will maintain the user account and password which will be used for communicating with SharePoint Online sites. 
 

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>
  <appSettings>
    <add key="SPOAccount" value="*******@onlysharepoint2013.onmicrosoft.com" />
    <add key="SPOPassword" value="**************" />
  </appSettings>
</configuration>

The app.config file looks like below:

https://www.enjoysharepoint.com/wp-content/uploads/2018/07/sharepoint-webjobs.png

Now in the Program.cs page we will write the code which will add an item to a SharePoint online list. The code looks like below:

Program.cs:

using Microsoft.SharePoint.Client;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Security;
using System.Text;
using System.Threading.Tasks;
 
namespace DemoAzureWebJobsForSharePoint
{
    class Program
    {
        static void  Main(string[] args)
        {
            using (ClientContext context = new ClientContext("https://onlysharepoint2013.sharepoint.com/sites/Bhawana/"))
            {
                context.AuthenticationMode = ClientAuthenticationMode.Default;
                context.Credentials = new  SharePointOnlineCredentials(GetSPOAccountName(), GetSPOSecureStringPassword());
                var list = context.Web.Lists.GetByTitle("MyDemoWebjobsList");
                ListItemCreationInformation newItemCreateInfo = new  ListItemCreationInformation();
                Microsoft.SharePoint.Client.ListItem newItem = list.AddItem(newItemCreateInfo);
                newItem["Title"] = "Item added by Azure WebJobs";
                newItem.Update();
                context.ExecuteQuery();
            }
        }
 
        private static  SecureString GetSPOSecureStringPassword()
        {
            try
            {
                var secureString = new  SecureString();
                foreach (char c in ConfigurationManager.AppSettings["SPOPassword"])
                {
                    secureString.AppendChar(c);
                }
                return secureString;
            }
            catch
            {
                throw;
            }
        }
 
        private static  string GetSPOAccountName()
        {
            try
            {
                return ConfigurationManager.AppSettings["SPOAccount"];
            }
            catch
            {
                throw;
            }
        }
    }
}

Deploy WebJobs to Microsoft Azure: (Method-1)

Once the development is over we need to deploy the WebJobs to Microsoft Azure. To deploy the Azure webjob we need to create a web apps in Microsoft Azure and we need to get the Publish Profile for the web app. Follow the below articles and get the publish profile which we will need later.

Now right click on the Project and click on "Publish as Azure WebJob..." like below:

https://www.enjoysharepoint.com/wp-content/uploads/2018/07/sharepoint-online-webjobs.png

In the next screen in the Add Azure WebJob, You can provide the WebJob name and also you can schedule the job according to your requirement like below. Here we have just set the run mode as "Run Continuously" like below:

https://www.enjoysharepoint.com/wp-content/uploads/2018/07/sharepoint-online-webjobs-schedule.png

Now in the Publish Web dialog box, click on Import like below:

https://www.enjoysharepoint.com/wp-content/uploads/2018/07/sharepoint-online-webjobs-publish.png

Then browse to the profile which we have imported through the above url like below:

https://www.enjoysharepoint.com/wp-content/uploads/2018/07/sharepoint-online-webjobs-publish-microsoft-azure.png

Then it will populate all the details like below. You can also click on Validate Connection button to validate the connection like below.

https://www.enjoysharepoint.com/wp-content/uploads/2018/07/deploy-webjobs-to-microsoft-azure.png

Once it will published successfully, if we will open the list, we can see few items has been added like below:

https://www.enjoysharepoint.com/wp-content/uploads/2018/07/deploy-webjobs-to-microsoft-azure-sharepoint.png

Review the WebJob:

We can also review the WebJob in Microsoft Azure Portal. Login to Microsoft Azure Portal and select the Web App and then select WebJobs and then you can see WebJob running like below:

https://www.enjoysharepoint.com/wp-content/uploads/2018/07/deploy-webjobs-to-microsoft-azure-sharepoint-online.png

Deploy WebJobs to Microsoft Azure: (Method-2)

We can also deploy webjobs to Microsoft Azure using upload zip file method. Follow the below article for detailed steps:

References:

You can also check few useful articles:

Conclusion:

In this post we have discussed how we can create an Azure WebJobs and how we can deploy the webjobs to Windows Azure and how we can use it for SharePoint Online Office 365 sites.