Posted in Dynamics 365, Power Apps Portals

Power Apps Portals – Identify Portal User via Plug-in

If you work with D365/Dataverse for some time, you know how Plug-ins can really extend the platform and open a whole new world of possibilities (if you are new to Dataverse plug-ins, make sure to check out the official documentation: https://docs.microsoft.com/en-us/power-apps/developer/data-platform/write-plug-in).

Microsoft has released a while ago a new PluginExecutionContext class that contains additional details related to Power Apps Portals. This is very useful if you need to identify if the operation is coming from the Portals and to identify the User (Contact) that is performing the action.

This link contains more details of the class: https://docs.microsoft.com/en-us/dotnet/api/microsoft.xrm.sdk.ipluginexecutioncontext2?view=dataverse-sdk-latest

The usage of this is very simple, just initiate a new object for the IPluginExecutionContext2 class and you can get access to the IsPortalsClientCall and PortalsContactId properties:

using Microsoft.Xrm.Sdk;
using System;

namespace OR.Plugins
{
    public class PortalPlugin : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            IPluginExecutionContext2 context = (IPluginExecutionContext2)serviceProvider.GetService(typeof(IPluginExecutionContext2));

            bool isPortalsCall = context.IsPortalsClientCall;

            if (isPortalsCall)
            {
                Guid portalsContactId = context.PortalsContactId;
                if (portalsContactId == Guid.Empty)
                {
                    // anonymous
                }
                else
                {
                    // authenticated
                }
            }
            else
            {
                // not coming from portals
            }
        }
    }
}

In the below example, I am creating a task related to the current record (Account in my case) and setting details in the subject of the task as if this is coming from the Portals or not:

using Microsoft.Xrm.Sdk;
using System;

namespace OR.Plugins
{
    public class PortalPlugin : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            IPluginExecutionContext2 context = (IPluginExecutionContext2)serviceProvider.GetService(typeof(IPluginExecutionContext2));
            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService orgService = serviceFactory.CreateOrganizationService(context.UserId);
            string subject = "";

            bool isPortalsCall = context.IsPortalsClientCall;

            if (isPortalsCall)
            {
                Guid portalsContactId = context.PortalsContactId;
                if (portalsContactId == Guid.Empty)
                {
                    subject = "Coming from Portals - Anonymous user";
                }
                else
                {
                    subject = $"Coming from Portals - Authenticated user: {portalsContactId}";
                }
            }
            else
            {
                subject = "Not coming from Portals";
            }

            Entity followup = new Entity("task");

            followup["subject"] = subject;
            followup["scheduledstart"] = DateTime.Now.AddDays(7);
            followup["scheduledend"] = DateTime.Now.AddDays(7);
            followup["regardingobjectid"] = new EntityReference(context.PrimaryEntityName, context.PrimaryEntityId);

            orgService.Create(followup);
        }
    }
}

Then when updating my Account record in three different ways (Model-Driven App / Portals authenticated / Portal unauthenticated), here is the result:

Hope this tip is useful for you and your Power Apps Portals/Dataverse implementation.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s