How to trigger a plug-in in Dynamics 365 Customer Engagement when Product License is updated in Office 365


In this blog, I will demonstrate how to trigger a plug-in in Dynamics 365 Customer Engagement when Product License is updated in Office 365 for a Dynamics 365 user.

1.    Write a simple plugin to update the field created in the previous steps

using System;

using System.Linq;

using Microsoft.Xrm.Sdk;

namespace Plugins

{

public class UserUpdateAsync: IPlugin

    {

public void Execute(IServiceProvider serviceProvider)

        {

            var tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

            if(tracingService == null)

            {

                throw new InvalidPluginExecutionException("Failed to retrieve the tracing service.");

}

tracingService.Trace("Started UserUpdateAsync Plugin");

             var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

             if (context.PreEntityImages.Contains("PreImage"))

            {

tracingService.Trace("------------------");

tracingService.Trace("PRE IMAGE ATTRIBUTES");

                var user = context.PreEntityImages["PreImage"];

                foreach (var attribute in user.Attributes.Where(attribute => attribute.Key == "userlicensetype"))

                {

tracingService.Trace($"Key: {attribute.Key} - Value: {attribute.Value}");

                }

            }

             if (context.PostEntityImages.Contains("PostImage"))

            {

tracingService.Trace("------------------");

tracingService.Trace("POST IMAGE ATTRIBUTES");

                var user = context.PostEntityImages["PostImage"];

                foreach(var attribute in user.Attributes.Where(attribute => attribute.Key == "userlicensetype"))

                {

tracingService.Trace($"Key: {attribute.Key} - Value: {attribute.Value}");

                }

            }

tracingService.Trace("Executed UserUpdateAsync Plugin");

        }

    }

}

*Please be aware that best practices were not used in the construction of this plugin and it is only intended to test this functionality.

2.    Register a plugin step as follow:

3.    Register PreImage and PostImage for the plugin step registered in step #1

4.     Turn on Plug-in Trace Log, for more information check the article Debug a plug-In

5.     Login in the https://admin.microsoft.com/AdminPortal page

6.     Navigate to Users > Active Users

7.     Pick any user, click on the Edit button for the Product Licenses option

8.     Switch the licenses for Dynamics 365 Customer Engagement to Off, click on the Save button

9.     The synchronization between Office 365 and Dynamics 365 Customer Engagement could take some time, but once it synchronizes, go to Settings > Plug-in Trace Log, you will see an entry for the plug-in triggered by the action from step #8

 

10.  Open the correct entry and check the Message block under the Execution section, it will be easier to visualize it, if you copy and paste it in a text editor like Notepad

11.  Noticed that as you removed the licenses for a specific user, the attribute userlicensetype changed from 6 to -1

Started UserUpdateAsync Plugin

------------------

PRE IMAGE ATTRIBUTES

Key: userlicensetype - Value: 6

------------------

POST IMAGE ATTRIBUTES

Key: userlicensetype - Value: -1

Executed UserUpdateAsync Plugin

Comments (0)

Skip to main content