Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM forum
Unanswered

Share Record using a plugin

Posted on by 92
Hello Community,
 
I am facing difficulty in sharing a record using a plugin. I will paste the function below.
The code is not returning any error and executing normally, but the records are not being shared. The user ID and the record ID are passed.
 
I tried to debug the code and it is working fine without any errors, but without sharing.
 
Can someone assist me with any error in my function or another approach for sharing?
 
public void ShareRec(Mission mission, List<RecordToShare> recs)
{
    foreach (var userId in mission.MissionPrev.User)
    {
        var userReference = new EntityReference(/systemuser/, new Guid(userId));
        foreach (var privilege in mission.MissionPrev.Privileges)
        {
            var accessMask = Models.Roles[privilege.Id];
            foreach (var record in recs)
            {
                var recordReference = new EntityReference(record.EntityName, record.RecordId);
                var grantAccessRequest = new GrantAccessRequest
                {
                    PrincipalAccess = new PrincipalAccess
                    {
                        AccessMask = accessMask,
                        Principal = userReference
                    },
                    Target = recordReference
                };
                try
                {
                    service.Execute(grantAccessRequest);
                    // Handle success or logging here if needed
                }
                catch (Exception ex)
                {
                    throw new InvalidPluginExecutionException(ex.Message);
                    // Handle the exception (logging, error handling, etc.)
                }
            }
        }
    }
}

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,537 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,520 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans