Category Archives: Azure Functions

Creating an Azure Function to act as a SharePoint Timer Job

High level steps:

1. Create a new Azure Function in the Azure Portal
2. Upload the SharePoint Client dlls
3. Configure AppSettings for the username, password, and other fields as desired
4. Write and execute PowerShell!

Create a new Azure Function in the Azure Portal

This is documented lots of places, including a hello world example in the docs, but here’s a summary of the steps:

1. Log into the azure portal
2. Click the green plus sign to add a new service, and search for “function”.
3. Click “function app” in the search results, and then click create in the next dialog
4. Fill in the standard azure options dialog, specifying subscription, resource group, etc.
5. Wait for it to be created, and then navigate to your new function app.
6. On the “Get started quickly with a premade function” page, don’t click any of the large boxes. Instead, click “create your own custom function”.
7. That should take you to a page with a scrollable list of a bunch of different options. In the Language filter at the top, pick “PowerShell”.
8. Select TimerTrigger-Powershell
9. Give it a name and a schedule. (careful, the default schedule is to run it every 5 minutes! As noted below, MS provides documentation of examples of different entries for this field to run the function at different times.)

Ok, at this point you should have an Azure Function that uses PowerShell and is configured to run every 5 minutes (or on whatever schedule you specified). You can also manually run it via the “Run” button, and you should see the output in the console at the bottom of the screen.

A few details before going on to the next step:
1. To change the schedule, click on “Integrate”
2. To see example of different scheduling options, on the “Integrate” page, click on “documentation”
3. To pause it so it’s not running every x minutes, click on “Manage”, and then “disable” at the top
4. To see how many times it has run, click on “Monitor”.
5. To get to a huge list of other options that are typical for Azure, click on the name of your function app, (on the left side, above your function, above “Functions”), and then click “Platform Features”

Upload the SharePoint Client dlls

At this point, you can write whatever PowerShell you want, but if you want to use the SharePoint Client dlls, you’ll need to upload them. Though, the first step is finding them. They should be in:

C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI

Of course, you can also download them from here:

Specifically, you’ll need to find the following two dlls:

There’s a couple ways to get the dll files uploaded. You could use FTP or Kudu. A nice walkthrough of using FTP is here. A walkthrough using Kudu is here. Following is the quick explanation for Kudu, which seems easier to me:

1. Click on the name of your function app, (on the left side, above your function, above “Functions”), and then click “Platform Features”
2. Under Development Tools, click “Kudu”
3. In the top nav, select debug console –> PowerShell
4. In the top panel, click on “site” –> “wwwroot” –> nameofyourfunction
5. either use the PowerShell window to create a new directory named “modules”, or click the big plus sign towards the top of the screen and select “new folder”, and name it “modules”
6. click the modules folder. At this point, just drag and drop files from windows explorer onto the table with the “name”, “modified”,”size” headers. Specifically, drop both files, mentioned above.

Actually, another way to upload files is just to use the “View Files” tab in the panel on the right in the screen where you can work on your function. However, at this time, I don’t know of a way to create a folder from that panel, and we need the dlls to be in the “modules” folder.

Also, if you like the PnP project, all of these instructions work for those PowerShell commands as well. Just find the install folder for PnP, which is something like:
C:\Program Files\WindowsPowerShell\Modules\SharePointPnPPowerShellOnline

It should have a couple dozen dlls, including OfficeDevPnP.Core.dll. Just upload all the dlls in that directory, and the PnP PowerShell cmdlets should work just fine.

Configure AppSettings for the username and password

There’s a nice writeup of this here, but here’s a short list of the basic steps:

1. Get back to the Platform Features page
2. Under General Settings, click “Application Settings”
3. In the panel on the right that appears, scroll down to the “App settings” section.
4. Add keys and values as needed, then don’t forget to click “save” at the top. For my code example below, I’ve created two settings, “O365User” and “pwd”

Write and execute PowerShell!

Ok, all the prereqs should be in place. Next is the actual function code. So, get back to the function where there was one line of sample code with one line that writes out the date. Leave that line there, and paste in the following, modifying the URL to point to your SharePoint online site. This code is pretty much the basic hello world example from Microsoft’s getting started page¬†for CSOM.

Write-Output "PowerShell Timer trigger function executed at:$(get-date)";
$userId = $env:O365User
$pwd = ConvertTo-SecureString $env:pwd -AsPlainText -Force
$url = ""

$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$clientContext.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userId, $pwd)

if (!$clientContext.ServerObjectIsNull.Value)
Write-Output "Connected to SharePoint Online site: '$Url'"

# The SharePoint web at the URL.
$web = $clientContext.Web;

# We want to retrieve the web's properties.

# Execute the query to the server.

# Now, the web's properties are available and we could display
# web properties, such as title.
Write-Output $web.Title;

Lastly, don’t forget that is is running on a timer, and so you’re paying for it at this point. So disable it or delete it to stop the charges.