SharePoint List Operation With Time Trigger Azure Function

Before coming on to the Time Trigger Azure let's understand the Azure Function first,

Azure Function

Azure Functions is a service that lets you launch serverless applications and run event-triggered code on Microsoft Azure without the need to explicitly manage infrastructure.

Azure Functions enables you to write serverless code to handle events at scale, with minimal overhead and cost.

Time Trigger Azure Function

Timer-triggered Azure functions allow you to run a function on a configurable schedule. Unlike HTTP-triggered Azure functions, timing info is stored in an Azure storage account and then passed to the function on execution.

Overview

In this article, we will cover SharePoint List CRUD Operation using Time Trigger Azure Function. There can be many occasions where we need to perform scheduled operations with SharePoint Online Lists. In On-Premises we used to have Timer Job to perform any custom operations. Time Trigger Azure Function can be used in similar cases.

Since it will schedule operation, there will not be any user interactions. We will Client ID and Client Secret to authenticate. We will be using SharePoint app-only authentication.

Follow below points,

Open the appregnew.aspx page. click on the Generate button to generate a client ID and client secret.

https://{tenant name}.sharepoint.com/_layouts/15/appregnew.aspx

Note down the retrieved information (client ID and client secret). We will need this in the next steps.

In the Title field, type the name of the App. For App Domain type www.localhost.com and to Redirect URI, type https://www.localhost.com

Create Time Trigger Azure Function Project

For the sake of this article, we are going to create C#.net-based Azure function but you can choose to do it in other supported languages also...

Open Visual Studio (not code) - Create a new Project and Choose Azure Functions.

  • Choose Project name and solution path and click Next it will ask you the trigger type and .NET version(as in the below screenshot).
  • Choose Azure Function and Time trigger in Function Dropdown

  • It would create the project and you should see something like below

  • First change Run Method Argument – Change it to
    Run([TimerTrigger("0 * * * * *")]TimerInfo myTimer, ILogger log)
    TimerTrigger("0 * * * * *") – This means that this function will triggered in every min

    To get SharePoint List data, we need to install supportive packages from NuGet Manager. Go to Tools-> NuGet Package Manager - Manage NuGet Packages for Solutions

  • Browse “Microsoft.SharePoint.Client.latest” and install it.

  • Browse “PnP.Framework” and install it.

Once it is installed it will be visible under Installed tab.

  • We are all set to write our code to get SharePoint List data.
  • Initialize Site URL (Your site URL), Client ID and Client Secret Value (Values we have noted earlier).

Use below code,

using System;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;
using Microsoft.SharePoint.Client;
using System.Linq;
using PnP.Framework;
namespace AFGetListItem {
    public class Function1 {
        [FunctionName("Function1")]
        public void Run([TimerTrigger("0 * * * * *")] TimerInfo myTimer, ILogger log) {
            log.LogInformation($ "C# Timer trigger function executed at: {DateTime.Now}");
            try {
                string siteUrl = "Your Site Url";
                string ClientSecret = "YourClientID";
                string ClientId = "ClientSecret";
                using(var clientContext = new AuthenticationManager().GetACSAppOnlyContext(siteUrl, ClientId, ClientSecret)) {
                    Web oWebsite = clientContext.Web;
                    List list = oWebsite.Lists.GetByTitle("CustomList");
                    clientContext.Load(list);
                    clientContext.ExecuteQuery();
                    CamlQuery query = CamlQuery.CreateAllItemsQuery();
                    ListItemCollection items = list.GetItems(query);
                    clientContext.Load(items);
                    clientContext.ExecuteQuery();
                    string result = " CustomList items Count: " + items.Count();
                    log.LogInformation(result);
                    foreach(ListItem itm in items) {
                        Console.WriteLine("Title: " + itm["Title"]);
                    }
                }
            } catch (Exception ex) {
                log.LogInformation($ "C# Timer trigger function executed Exception at:" + ex.Message);
            }
        }
    }
}

Test the Project

Run the Project using F5 and make sure you are seeing below the window


Similar Articles