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://thepowerappsninja.com/wp-admin/post-new.php
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://thepowerappsninja.com/wp-admin/post-new.php
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:
usingMicrosoft.Xrm.Sdk;usingSystem;namespaceOR.Plugins
{
publicclassPortalPlugin : IPlugin {
publicvoidExecute(IServiceProvider serviceProvider)
{
IPluginExecutionContext2 context = (IPluginExecutionContext2)serviceProvider.GetService(typeof(IPluginExecutionContext2));
boolisPortalsCall = 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:
usingMicrosoft.Xrm.Sdk;usingSystem;namespaceOR.Plugins{
publicclassPortalPlugin : IPlugin {
publicvoidExecute(IServiceProvider serviceProvider)
{
IPluginExecutionContext2 context = (IPluginExecutionContext2)serviceProvider.GetService(typeof(IPluginExecutionContext2));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory)); IOrganizationService orgService = serviceFactory.CreateOrganizationService(context.UserId);
stringsubject = "";
boolisPortalsCall = 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 = newEntity("task");
followup["subject"] = subject;
followup["scheduledstart"] = DateTime.Now.AddDays(7);
followup["scheduledend"] = DateTime.Now.AddDays(7);
followup["regardingobjectid"] = newEntityReference(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:

