How to Create Azure Function APP in Azure Portal and Trigger an EXE


Sathish Nadarajan
SharePoint MVP
Published On :   06 May 2018
Visit Count
Today :  3    Total :   644



In most of the times, dealing with Office 365 scheduled jobs, we are supposed to write some sort of Console Applications (EXEs) which will be scheduled to execute periodically from the Windows Task Scheduler of the Application Server. But, in recent days, customers do not want to maintain a Server for this application either don’t want to keep this Executables in their available server along with some other application, for the sake of isolation.

Hence, the other way is to host this EXE as a function APP in the Azure and configure the Function APP based on our timings.

In this article, let us see how to do that with a sample EXE which will insert a record on a SharePoint List. i.e., whenever the Function APP Executes, it will insert a record a list.

Note: There are various types of Triggers available for the Function APP. In this scenario, we are going to look at the “Timer Trigger”. In most of our requirements, we will be using either “Timer Trigger” or the “HTTP Trigger”

Now, let us see the step by step procedures.

1. Login to the Azure Portal.

2. Go to the Resource group which we have. In my case, I have a Resource Group called “Demo”

clip_image002

3. Click on the Add Button and type “function app”

clip_image004

4. Select the Function APP from the list.

clip_image006

5. Enter the details of the Function APP and Click Create.

clip_image008

6. Once, the function app got created, go to the function app and click Add functions.

clip_image010

7. As we said in the beginning, let us create a function with a Timer as trigger.

clip_image012

8. Once, it got created, we can see the default 3 files as below.

clip_image014

9. The functions.json is used to give the input and the configuration.

10. The Run.csx is the actual file, which can be treated as our Program.CS in the console application.

11. By default, a sample code will be given on the run.csx.

 using System;
 
 public static void Run(TimerInfo myTimer, TraceWriter log)
 {
     log.Info($"C# Timer trigger function executed at: {DateTime.Now}");
 }
 

12. Replace the code with the below line of code.

 using System;
 using System.Diagnostics;
 
 public static void Run(TimerInfo myTimer, TraceWriter log)
 {
     log.Info($"C# Timer trigger function Started at: {DateTime.Now}");
 
     
     string functionName = "TimerTriggerCSharp1";
 
     var workingDirectory = Path.Combine(@"d:\home\site\wwwroot", functionName);
     Directory.SetCurrentDirectory(workingDirectory);//fun fact - the default working directory is d:\windows\system32
 
     Process.Start("FunctionAPP.InsertRecord.exe");
     
     log.Info($"C# Timer trigger function running at: {DateTime.Now}");
     
     log.Info($"C# Timer trigger function Ended at: {DateTime.Now}");
 }
 

13. On the screen, it will look like

image

14. On the right hand side, click on the “Upload” button and upload all the necessary files for the EXE to execute. Usually, I will be uploading all the files under bin/debug or bin/release.

15. After uploading the files, click on Run. The EXE will be executed and a new record will be inserted on the My Demo list.

16. To configure the timer job, go to “Integrate” section and update the value on the Schedule.

clip_image018

Very straight forward. With this, we don’t require any physical server to host a repetitive tasks done on the SharePoint Office 365.

Happy Coding,

Sathish Nadarajan.

SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Categories

KWizCom Scan