allBlogsList

Calling Azure Functions from Content Hub

Intro

This post provides a step-by-step walkthrough on setting up all needed elements to enable Content Hub to invoke Azure Functions, which can be very handy in integration scenarios where changes in Content Hub would initiate running light processes in Azure.

Azure Functions in Content Hub

Content Hub provides a simple, no-code, yet powerful way of integrating with 3-rd party systems by calling external REST APIs and passing optional payload. An action of type API Call can be used to define how API call is initiated from Content Hub. Such external API can reside anywhere on the web on-premise or in the Cloud, the only requirement is that it needs to be made accessible to Content Hub, which runs in the Cloud. Azure Function is a very lightweight and cost-effective way of encapsulating system logic into readily available and lightweight blocks of code, which don't require hosting service in order to be available and executed on demand (Microsoft Azure takes care of instantiating required container(s) when Azure function is invoked). Microsoft provides excellent documentation on Azure functions, which can be found here.

 Below high-level steps describe how to implement the following logic flow in Content Hub and Azure:

  1. Entity Change Event: a certain entity is created or updated in Content Hub
  2. Trigger: when trigger conditions are met then invoke pre-defined Action
  3. Action: read data from the affected entity and invoke a REST API call, passing entity data in the payload
  4. REST API Call: Content Hub calls REST API endpoint via HTTP POST, the passing payload in JSON body
  5. Execute Azure Function: Azure Function is triggered in Azure, function code reads message data and executed business logic (in this case, just log some message data)

Create Azure Function

More details on creating and managing Azure Functions can be found here.

In Azure Portal head to Azure Marketplace, search for Azure Function, then choose to create one. I chose to use Code and .NET Core, like so: custom

custom

Create HTTP Endpoint

Detailed steps on creating and managing Azure Function HTTP endpoint can be found here. Here's mine: custom

Enter Function Code

Head to the "Code + test" section of newly created Azure Function and enter code like so (feel free to change as needed, this code is just a "getting started" example): custom

Function code in text format:

#r "Newtonsoft.Json"
using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;

public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
    log.LogInformation("C# HTTP trigger function processed a request.");
    string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
    dynamic data = JsonConvert.DeserializeObject(requestBody);
    string title = data?.Title;
    string quote = data?.Quote;
    string body = data?.Body;
    log.LogInformation($"Received API call from Content Hub at {DateTime.Now}. Title: {title}, Quote: {quote}, Body: {body}. Message: {requestBody}");
    string responseMessage = string.IsNullOrEmpty(title)
    ? "This HTTP triggered function executed successfully. Pass input parameters in the request body."
    : $"Received API call from Content Hub at {DateTime.Now}. Title: {title}, Quote: {quote}, Body: {body}. Message: {requestBody}";
    return new OkObjectResult(responseMessage);
}

Save Function and Copy Function URL

  • Click on "Save" and then "Get function URL" link and copy the full string to use on the next step
  • Expand Logs at the bottom left of the function pane to start capturing logged messages
  • Leave browser window open, we'll get back here once the Content Hub portion is done to test the results

Create API Call Action and Trigger in Content Hub

Now to the Content Hub part: I will create an "API call" Action and define the trigger to call this action every time when an entity of a given type is created, updated, or deleted in Content Hub.

Create API call Action

In Content Hub head to the Admin page, click on Actions, and click "New Action". Populate a new Action name and paste the function URL from the previous step into the API URL field. Choose the POST Method and Values as you see fit for your given entity definition. In this example, I'm using the M.Content entity where Content Type is Blog (One of the Out-of-the-box content types coming with standard Content Hub schema). custom

Create Trigger

As per Sitecore Content Hub documentation: Sitecore Content Hub provides users with a Trigger framework, which allows them to configure triggers via the User Interface (UI). A trigger is a set of actions that are automatically executed after specific events and under specific conditions.

A trigger consists of 3 components:

  • Event: The event to which the trigger should react.
  • Conditions: The conditions under which the trigger's actions are executed.
  • Actions: The actions that the trigger executes.

Head to the Admin page, select triggers, and click "New Trigger". Below screenshots show how Event, Condition, and Actions can be configured for sending messages about M.Content changes using above created Service Bus and Content Hub Action

  • General tab defining the trigger to be run on entity create, update, and delete. Execution type set to "In background", which is recommended in most cases to avoid trigger event interfering with the actual process and affecting its performance. custom

  • Condition, specifying that trigger will only fire when entity type is M.Content and its "Content Type" property is "Blog". custom

  • And finally, the Action to execute is the one we created on previous steps (it's added via the "Add Action" link). Note that we're not limited to just one action, Content Hub can be configured to execute multiple actions in the same trigger, e.g. send message to Azure Service Bus and then send a notification email to admin users. [[This blog post describes how to send emails from Content Hub.]] custom

Test Everything

Now, if everything had been configured correctly, we should be able to update an Entity in Content Hub and see its values processed by Azure Function. Our function, again, is called via REST API by Content Hub Action, invoked by change Trigger…

  • Head back to Azure Portal, navigate to Azure Function, created on previous steps, choose "Code + Test" and expand Logs at the bottom left of the function pane to start capturing logged messages
  • Switch to Content Hub and create new or update an existing entity (in this case I used M.Content of type Blog). This change can be made from the custom entity page or from the Admin -> Entities page.
  • Withing 10-20 seconds the Content Hub will trigger the function, which should result in a log message like so: custom

References

https://docs.microsoft.com/en-us/azure/azure-functions/functions-create-first-azure-function