Blog Securely trigger an Azure WebJob through Microsoft Flow RSS Feed

Securely trigger an Azure WebJob through Microsoft Flow

Jul 26, 2019

Azure WebJobs

WebJobs is an Azure App Service feature that provides an easy way for users to run code or script as a background process in the same context as an app service web app, API app or mobile app. WebJobs support C#, PowerShell, JavaScript, PHP, TypeScript, Bash, Python, Bash, .bat, .cmd and more which means that a WebJob can run any code or script that can execute in App Service sandbox. It supports NPM and NuGet. There is no extra cost to use WebJobs.

Note: WebJobs isn’t yet supported for App Service on Linux.

Users can use Azure WebJobs to develop timer job functionality or Windows Task Scheduler to carry out tasks in SharePoint Online for an overall Sharepoint development. For instance, you may require scheduling a timer job to retrieve data added in a SharePoint list to a database. It is important to note that SharePoint Online does not provide luxury to deploy farm solutions and timer jobs to iteratively perform scheduled tasks. To schedule a similar timer job in SharePoint Online, you require to implement a console application as an Azure WebJob. The console application uses the client-side object model (CSOM) to access SharePoint Online resources.

If you require to fill the need of custom background process need, Azure WebJobs is what you require.

Types of WebJobs

There is two type of Azure WebJobs: Continuous and Triggered. Refer the below table for the differences between them.

Continuous Triggered
It starts as soon as the WebJob is configured.

The job keeps on running in an endless loop to avoid itself from ending.
In case, if the job ends, you can restart it.

Starts on a manual trigger or on a schedule.
Runs on all the instances that a web app runs on. Runs on a single instance that Azure chooses for load balancing.
Provides a support for remote debugging. Doesn’t provide support for remote debugging.

File Types Supported for scripts or code

  • .ps1 (PowerShell)
  • .js (Node.js)
  • .cmd, .bat, .exe (Windows cmd)
  • .php (PHP)
  • .sh (Bash)
  • .jar (Java)
  • .py (Python)

Microsoft Flow

Microsoft Flow is the top choice to do any automated task in Office 365. It is an online workflow service that automates activities over the most widely recognized apps and services. It is regularly used to automate work processes between your preferred applications and services, synchronize records, get notifications, gather data, and significantly more. 

When you sign up, you can interface with, in excess of 220 services and can manage data either in the on-premises or in cloud sources like SharePoint and Microsoft SQL Server. The list of applications you can use with the Microsoft Flow grows constantly. 

For instance, you can automate below type of tasks: 

  1. Instantly react to high-need warnings or messages. 
  2. Capture, track, and catch up with new sales customers. 
  3. Copy all email attachments to your OneDrive for Business account. 
  4. Collect information about your business, and shareoffer that data with your group. 
  5. Automate approval work processes.

For reference Please follow this blog to know more MS Flow.

Let’s call a Triggered Azure WebJob (manual/scheduled WebJob) through MS Flow. But before that one must have an active Azure and Office 365 subscriptions.

Azure WebJobs for Office 365 sites

Here, we will see basic concepts regarding how to build custom jobs for Office 365 sites and Azure WebJob to act as a scheduled job for your Office 365 SharePoint sites.

All you require is to build a new WebJob is to create a console application.

Setup and run console application as Azure WebJob

To configure a console application to run as an Azure WebJob, perform below steps:

  • Select New Project > Visual C# > Console Application > Name: WebJobSample > OK in Visual Studio. Also, make sure to select the .NET Framework 4.5 or later.

New Project

Note: Before you start implementing a console application, make sure ensure to add required assemblies in the Visual Studio.

  • Install SharePoint specific assemblies from NuGet. Navigate to Tools > NuGet Package Manager > Manage NuGet Packages for Solution

NuGet Package Manager

  • Browse for App for SharePoint > Select package called AppForSharePointWebToolkit > Click Install.

Install App For SharePoint WebToolkit

  • Select OK to install dependencies and required helper classes.

Install dependencies

Verify that the NuGet package was successfully installed by checking below the new helper classes, created in your console application.

Verify that the NuGet package

  • Open App.config file and store your Office 365 account credentials by adding appSettings element as shown below to fetch them easily inside your code. Add SPOAccount and SPOPassword keys inside appSettings element to store username and password of your tenant account.
App.config
<?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="  "/>
    <add key="SPOPassword" value="  "/>
  </appSettings>
</configuration>

Note: Here, the config file stores organization’s username and password in clear text. It depends upon your project requirements for where and how to store authentication details and encrypting the passwords for production deployments.

  • Add below code inside Program.cs file of the created console application.
  • Import using statements
  • Program.cs
    using Microsoft.SharePoint.Client;
    using System.Security;
    using System.Configuration;
    using System;
  • Configure following methods inside your class:
  • Main: Used to authenticate your SharePoint site and execute CSOM requests to carry out tasks inside your site. We will get a SharePoint library using CSOM and then get the total number of documents inside that library as an output.
  • Program.cs
    static void Main(string[] args)
            {
                using (ClientContext context = new ClientContext(" "))
                {
                    // Use default authentication mode.
                    context.AuthenticationMode = ClientAuthenticationMode.Default;
                    context.Credentials = new SharePointOnlineCredentials(GetSPOAccountName(), GetSPOSecureStringPassword());
     
                    // Add your CSOM code to perform tasks on your sites and content.
                    try
                    {
                        List objList = context.Web.Lists.GetByTitle("Docs");
                        context.Load(objList);
                        context.ExecuteQuery();
     
                        if (objList != null && objList.ItemCount > 0)
    	                {
                            Console.WriteLine(objList.Title.ToString() + " has " + objList.ItemCount + " items.");
                        }
     
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("ERROR: " + ex.Message);
                        Console.WriteLine("ERROR: " + ex.Source);
                        Console.WriteLine("ERROR: " + ex.StackTrace);
                        Console.WriteLine("ERROR: " + ex.InnerException);
     
                    }
                }
            }
  • GetSPOAccountName: Create this method to read a username from the config file.
  • Program.cs
    private static string GetSPOAccountName()
            {
                try
                {
                    Console.WriteLine("Entered GetSPOAccountName.");
                    return ConfigurationManager.AppSettings["SPOAccount"];
                }
                catch
                {
                    throw;
                }
            }
  • GetSPOSecureStringPassword: Create this method to read a password from the config file.
Program.cs
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;
            }
        }

Publish your console application by running it as Azure WebJob

Once completed with implementing console application, you require to publish your console application as an Azure WebJob.

To publish your console application as an Azure WebJob, you can configure on-demand or triggered Azure WebJob.

Create WebJob binaries zip file to upload as Azure WebJob

You can upload a zip file containing output files from Visual Studio build (files in bin/Debug or bin/Release folder). This is a simple way of compiling and shipping your code.

Create WebJob binaries

Compress all the output files resulted from the building the process to create a zip file for your WebJob.

WebJob samples

Create an on-demand or manually triggered WebJob

  • Select App Services from left blade content and navigate to the App Service page of your App Service web app in the Azure portal. Note: You can use any sample app service to schedule your WebJob.

App Services

  • Click WebJobs.

Click WebJobs

  • On the WebJobs page, click Add.

WebJobs page

  • Provide Add WebJob settings as needed and click OK.

    • Name: A unique name, say “triggeredWebJob”, within the App service app. Make sure that the name must start with a letter or a number and should not contain special characters except “-” and “_”.
    • File Upload: Here upload the .zip file created earlier that contains the executable output files.
    • Type: Triggered
    • Triggers: Manual

Add WebJob

  • After a few seconds, the newly created WebJob appears on the WebJobs page of the App service.

Newly created WebJob

Trigger Azure WebJob through Microsoft Flow

We have a WebJob that counts the number of items inside a library. Such a job works perfectly for a scheduled job. Though, it can be ideal to trigger the web job automatically when a new document item is added instead of waiting for the scheduled job to run. You can carry out the process by triggering Azure WebJob through Microsoft Flow.

We require to fetch authentication token of the app service containing WebJob.

  • Retrieve authentication token for the app service where WebJob is running. Select Get publish profile from the ribbon on the app service overview page.

Get publish profile

It will download a file having PublishSettings extension. Browse for the publishProfile element with publishMethod="MSDeploy". Note the userName “$spWebJob” and password i.e userPWD in the file that will be used later while configuring a flow.

Publish Data

  • Fetch the Web Hook url by selecting your WebJob Properties.

Web Hook

  • Visit the MS Flow via https://flow.microsoft.com, select My flows and click New > Instant-from blank.

Visit the MS Flow

  • Add when a file is created (properties only) SharePoint trigger. Select Site Address and required Library Name as “Docs”.

SharePoint trigger

  • Add HTTP-HTTP action step. Rename the action to Trigger Azure WebJob. Change Method to "POST" for URI https://spwebjob.scm.azurewebsites.net/api/triggeredwebjobs/triggeredWebJob/run, which is the web hook URL retrieved earlier. No need to set Header and Body.
  • To view the advanced settings options, click on Show advanced options. Set Authentication to “Basic”, enter your Username and Password copied earlier.
  • Provide flow name as Call Azure WebJob and click Save. The final flow would look like the one below.

Provide flow name

  • On addition of a document inside the library, your flow will start running.

Flow will start running

  • Check the Flow run.

Check the Flow run

View the WebJob history

Once MS Flow runs successfully, you require to verify whether WebJob has been triggered successfully.

  • In the Azure Portal, navigate to the WebJobs page for your app service.

Azure Portal

  • Select triggeredWebJob WebJob under your app service for which you want to view job history and click the Logs button in the top ribbon.

WebJobs

  • It will open the WebJob Details page in a new browser tab. Choose the time for your job run to check details. Note the status of the job run to verify a successful run.

Webjob Details

  • View the text of log contents on the WebJob Run Details page to verify the job run based on your console application. It will return the number of documents inside the “Docs” library.

WebJob Run Details

Output Details

You can also view output details in a separate window by clicking the download link. You can also download output text itself by right-clicking download link and using browser options to save the output text of the job run.

Download Details

  • You can navigate back to the list of WebJobs by clicking WebJobs breadcrumb link at the top.

List of WebJobs

Microsoft Azure WebJobs

Conclusion

Try not to hold on out it an attempt, if a WebJob has the Web Hook endpoint!!! Provoke a triggered Azure WebJob automatically without hanging tight for its manual input/scheduled start time through MS Flow. MS Flow provisions us with different triggering actions like “When an item is created or modified”, “Item is deleted”, “When a record is selected”, etc. and furthermore provide other services like Dynamics 365, Salesforce, OneDrive, Dropbox, etc. to work around them alongside Azure WebJob. 

Allude our next blog  identified with the use of an Azure Function through the remarkable MS Flow.

 

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

About Tatvasoft

TatvaSoft is a CMMi Level 3 and Microsoft Gold Certified Software Development Company offering custom software development services on diverse technology platforms, like Microsoft, SharePoint, Biztalk, Java, PHP, Open Source, BI, Big Data and Mobile.

Follow Us