Hello,
I am facing the following problem: I would like to show a system user an appnotification when another user creates a share with this user (or a team in which the user is a member). During my research I found the following tutorial, which shows how a plugin can be triggered by a share / unshare:
Plugin Triggered When Share/Unshare CRM C#
When I try to register my plugin with the Plugin Registration Tool as shown in the tutorial, I already face the first problem: I can choose "GrantAccess" as my message, but I don't have "pa_class" for the primary entity . Also, I cannot specify "system user" here.
I know that the CreateNotification function works, since I already used it in another plugin. This is my code:
protected override void OnExecute(PluginContext context)
{
Post_Message_GrantOrRevokeAccess(context);
}
void Post_Message_GrantOrRevokeAccess(PluginContext context)
{
var log = context.Logger;
var pluginContext = context.PluginExecutionContext;
if (pluginContext.MessageName == "GrantAccess")
{
// Obtain the target entity from the input parameter.
EntityReference EntityRef = (EntityReference)pluginContext.InputParameters["Target"];
Microsoft.Crm.Sdk.Messages.PrincipalAccess PrincipalAccess = (Microsoft.Crm.Sdk.Messages.PrincipalAccess)pluginContext.InputParameters["PrincipalAccess"];
//Then got the User or Team and also Access Control that being Granted
//***to Get User/Team that being Shared With
var userOrTeam = PrincipalAccess.Principal;
var userOrTeamId = userOrTeam.Id;
var userOrTeamLogicalName = userOrTeam.LogicalName;
//use the logical Name to know whether this is User or Team
if (userOrTeamLogicalName == "team")
{
QueryExpression userQuery = new QueryExpression("systemuser");
userQuery.ColumnSet = new ColumnSet(true);
LinkEntity teamLink = new LinkEntity("systemuser", "teammembership", "systemuserid", "systemuserid", JoinOperator.Inner);
ConditionExpression teamCondition = new ConditionExpression("teamid", ConditionOperator.Equal, userOrTeamId);
// add the condition to the intersect
teamLink.LinkCriteria.AddCondition(teamCondition);
// add the intersect to the query
userQuery.LinkEntities.Add(teamLink);
//get the results
EntityCollection retrievedUsers = context.OrganizationService.RetrieveMultiple(userQuery);
log.LogTrace($"The ID of the team a share was created with: {userOrTeamId.ToString()}");
// fetch the results
foreach (Entity user in retrievedUsers.Entities)
{
var userId = user.Id;
CreateNotification(context, userId);
log.LogTrace($"The ID of the user a share was created with: {userId.ToString()}");
}
}
if (userOrTeamLogicalName == "systemuser")
{
CreateNotification(context, userOrTeamId);
log.LogTrace($"The ID of the user a share was created with: {userOrTeamId.ToString()}");
}
}
}
void CreateNotification(PluginContext context, Guid userId)
{
var notification = new Entity("appnotification");
notification["title"] = "Test Notification";
notification["body"] = $"User notification";
notification["ownerid"] = new EntityReference("systemuser", userId);
context.OrganizationService.Create(notification);
}
I also tried to create a profile to be able to debug my code, however, it seems as if the plugin is not even triggered yet.
This is not a surprise either, since my primary entity is not filled in the Registration Tool either.
I am thankful for any advice!