web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

Trigger a plugin to show a notification to a user if a share was created

(0) ShareShare
ReportReport
Posted on by 45

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!

I have the same question (0)
  • Verified answer
    Bipin D365 Profile Picture
    28,981 Moderator on at
    RE: Trigger a plugin to show a notification to a user if a share was created

    Hi,

    For Unshare you can still use 'RevokeAcess'.

    CR52.PNG

    Please mark my answer verified if this is helpful!

    Regards,

    Bipin Kumar

    Follow my Blog: xrmdynamicscrm.wordpress.com/

  • Verified answer
    Bipin D365 Profile Picture
    28,981 Moderator on at
    RE: Trigger a plugin to show a notification to a user if a share was created

    Hello,

    You will need to register your plugin on 'ModifyAccess' message.

    Please mark my answer verified if this is helpful!

    Regards,

    Bipin Kumar

    Follow my Blog: xrmdynamicscrm.wordpress.com/

  • Verified answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at
    RE: Trigger a plugin to show a notification to a user if a share was created

    Hey,

    I checked and it's correct that GrantAccess won't trigger the plugin - you should try the ModifyAccess message.

  • davalkan Profile Picture
    45 on at
    RE: Trigger a plugin to show a notification to a user if a share was created

    Hello, thank you very much for your support so far. Unfortunately, I still couldn't solve the problem.

    The problem doesn't seem to be my plugin itself, but with the registration of the plugin. If I register my plug-in for any other message than 'GrandAccess' such as 'Create' or 'Update', it will be triggered (and I get the corresponding error messages, of course).

    If I register the plug-in for the message 'GrandAccess', the plug-in is never triggered when a share is created. Regardless of whether I state 'none', 'opportunity' or anything else as the primary entity.

    How do I know for sure that it won't be triggered? I added an exception to my code right in the OnExecute method that is never thrown. Neither can I create a profile in the Registration Tool to debug the plugin locally, because the plugin is never triggered.

    Could it be that there is still a setting to be made in CRM here or something like that?

  • a33ik Profile Picture
    84,331 Most Valuable Professional on at
    RE: Trigger a plugin to show a notification to a user if a share was created

    I understand your scenario. The question is - what records are you sharing? Or you want to send notifications when you share any entity? If yes is the answer - use "none" in the Primary Entity.

  • davalkan Profile Picture
    45 on at
    RE: Trigger a plugin to show a notification to a user if a share was created

    Thank you very much Andrew for taking your time to help me!

    This makes absolute sense and I am sure that you are correct. However, I want to show the notification to a systemuser, whenever the GrandAccess is invoked for the systemuser. It is not necessary that I trigger the logic when creating a share with an account or a contact. The logic should trigger when a share is created with a systemuser and only inform the systemuser. Unfortunately I cannot select the systemuser as PrimaryEntity in the PluginRegistrationTool if GrantAccess is set as a message.

    Could it be that my problem cannot be implemented in this way?

  • a33ik Profile Picture
    84,331 Most Valuable Professional on at
    RE: Trigger a plugin to show a notification to a user if a share was created

    Hello,

    If my reply (or replies) answers your question take a minute and verify the answer. You can do it by clicking "Yes" at the top of the reply (or replies) under the label "Does this answer your question?"

  • a33ik Profile Picture
    84,331 Most Valuable Professional on at
    RE: Trigger a plugin to show a notification to a user if a share was created

    Hello,

    Use the entity sharing of which you plan to handle (like account or contact or whatever). pa_class is the schema name of the entity that Aileen used in her demo.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Andrés Arias – Community Spotlight

We are honored to recognize Andrés Arias as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Daniyal Khaleel Profile Picture

Daniyal Khaleel 147

#2
DAnny3211 Profile Picture

DAnny3211 134

#3
Abhilash Warrier Profile Picture

Abhilash Warrier 70 Super User 2025 Season 2

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans