Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM forum
Answered

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

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!

  • Verified answer
    Bipin D365 Profile Picture
    Bipin D365 28,962 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
    Bipin D365 28,962 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
    a33ik 84,321 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
    davalkan 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
    a33ik 84,321 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
    davalkan 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
    a33ik 84,321 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
    a33ik 84,321 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

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,558 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,645 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans