World is now on Opti ID! Learn more

KhurramKhang
Oct 24, 2017
  1463
(0 votes)

EPiServer integration with azure logic apps(part 1)

Microsoft Logic Apps provide a way to simplify and implement scalable integrations and workflows in the cloud. There are three major components that we will need to understand to before start working.

  1. Designer - It is relevantly independent module built in ReactJS to design workflows in Visual Studio, or in the Azure portal. Prepares workflow data model in JSON for us.
  2. Logic App Runtime - Responsible to run all the tasks retrieved from workflow definition in a distributed model.
  3. Connectors - Wrapped third-party APIs that allows us to use them.

How to connect EPiServer CMS or Commerce website with Azure Logic Apps. 

Azure logic app designer consists of connectors, triggers, actions and control flows, Its designer looks into open API/swagger of third party APIs and connectors and design cards to use in designing a workflow. Writing a managed connectors can be one way to integrate your EPiServer application, But we can use EPiServer.ServiceApi.Commerce.WebHooks or any webhook to Trigger the workflow and can use EPiServer.ServiceApi.Commerce or any custom API as Action

We presented a working demo at Episerver Close-up 2017 Sweden, Credits: Wessel Terpstra, where on submission of an order was used as a trigger and a custom promotion API was used to prepare a coupon code and post it mail this to the customer.

Image 2017-10-24_1710.png

Prepare your project

Install following packages

  • EPiServer.ServiceApi.Commerce.WebHooks
  • EPiServer.ServiceApi.Commerce
  • EPiServer.ServiceApi(For CMs Projects)
  • Swashbuckle

Create A Trigger

We need to post, purchase order(JSON) to Logic APP 

//NOT FOR PRODUCTION
[ServiceConfiguration(typeof(IWebHookManager), Lifecycle = ServiceInstanceScope.Singleton)]
    public class QuicksilverWebHookManager : IWebHookManager
    {
        public Task VerifyWebHookAsync(WebHook webHook)
        {
            return new Task(() => { });
        }

        public Task<int> NotifyAsync(string user, IEnumerable<NotificationDictionary> notifications, Func<WebHook, string, bool> predicate)
        {
            return Task.FromResult(default(int));
        }

        public async Task<int> NotifyAllAsync(IEnumerable<NotificationDictionary> notifications, Func<WebHook, string, bool> predicate)
        {
            int i = 0;
            var url = "Replace It with your settings"; //Webhook end point retrive from logic app designer when adding a wenhook triger
            foreach (var notification in notifications)
            {
                if (notification.Action != "OrderGroupUpdated" && (Type) notification["OrderGroupType"] != typeof(PurchaseOrder)) continue;
                
                using (var httpClient = new HttpClient())
                {
                    var json = JsonConvert.SerializeObject(notification);
                    var content = new StringContent(json, Encoding.UTF8, "application/json");
                    var result = await httpClient.PostAsync(url, content);
                    if (result.IsSuccessStatusCode) i++;
                }
            }

            return i;
        }
    }

Create a custom API (to use as Action):

Commerce service API offers a lot but few times those are not just enough and we require some custom API

[AuthorizePermission("EPiServerServiceApi", "WriteAccess"), RequireHttps, RoutePrefix("episerverapi/commerce/promotion")]
    public class PromotionApiController : ApiController
    {
        private static readonly ApiCallLogger Logger = new ApiCallLogger(typeof(PromotionApiController));
        private readonly IContentRepository _contentRepository;
        private readonly IPromotionEngine _promotionEngine;

        public PromotionApiController(IContentRepository contentRepository,  IPromotionEngine promotionEngine)
        {
            _contentRepository = contentRepository;
            _promotionEngine = promotionEngine;
        }

        /// <summary>
        /// Add a new coupon code and return back
        /// </summary>
        /// <param name="username">User name</param>
        /// <returns>returns coupon code</returns>
        [AuthorizePermission("EPiServerServiceApi", "WriteAccess"), HttpGet, Route("{username}")]
        [ResponseType(typeof(string))]
        public virtual IHttpActionResult GetCouponCode(string username)
        {
            Logger.LogGet("GetCouponCode", Request, new []{ username});
            
            try
            {
                var promotionId = ""; // Generate a coupon code
                return ContentReference.IsNullOrEmpty(promotionId) ? Ok("") : Ok(promoCode);
            }
            catch (Exception exception)
            {
                Logger.Error(exception.Message, exception);
                return InternalServerError(exception);
            }
        }
}

EPiServer integration with azure logic apps(part 2) by Wessel Terpstra


References: What are azure logic apps
Glossary: 

  • Workflow - Logic Apps provides a graphical way to model your business processes as a series of steps or a workflow.
  • Managed Connectors - Your logic apps need access to data and services. Managed connectors are created specifically to aid you when you are connecting to and working with your data. See the list of connectors available now in managed connectors.
  • Triggers - Some Managed Connectors can also act as a trigger. A trigger starts a new instance of a workflow based on a specific event, like the arrival of an e-mail or a change in your Azure Storage account.
  • Actions - Each step after the trigger in a workflow is called an action. Each action typically maps to an operation on your managed connector or custom API apps.
  • Enterprise Integration Pack - For more advanced integration scenarios, Logic Apps includes capabilities from BizTalk. BizTalk is Microsoft's industry-leading integration platform. The Enterprise Integration Pack connectors allow you to easily include validation, transformation, and more into your Logic App workflows.
Oct 24, 2017

Comments

Please login to comment.
Latest blogs
Make Global Assets Site- and Language-Aware at Indexing Time

I had a support case the other day with a question around search on global assets on a multisite. This is the result of that investigation. This co...

dada | Jun 26, 2025

The remote server returned an error: (400) Bad Request – when configuring Azure Storage for an older Optimizely CMS site

How to fix a strange issue that occurred when I moved editor-uploaded files for some old Optimizely CMS 11 solutions to Azure Storage.

Tomas Hensrud Gulla | Jun 26, 2025 |

Enable Opal AI for your Optimizely products

Learn how to enable Opal AI, and meet your infinite workforce.

Tomas Hensrud Gulla | Jun 25, 2025 |

Deploying to Optimizely Frontend Hosting: A Practical Guide

Optimizely Frontend Hosting is a cloud-based solution for deploying headless frontend applications - currently supporting only Next.js projects. It...

Szymon Uryga | Jun 25, 2025

World on Opti ID

We're excited to announce that world.optimizely.com is now integrated with Opti ID! What does this mean for you? New Users:  You can now log in wit...

Patrick Lam | Jun 22, 2025

Avoid Scandinavian Letters in File Names in Optimizely CMS

Discover how Scandinavian letters in file names can break media in Optimizely CMS—and learn a simple code fix to automatically sanitize uploads for...

Henning Sjørbotten | Jun 19, 2025 |