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 :
Microsoft Dynamics CRM (Archived)

Share records to only SalesPerson using Plugin in Dynamics CRM

(0) ShareShare
ReportReport
Posted on by 3,634

I want to share my contacts to only sales person who belongs to my child BU. I've done so far the following but I thought I am little de-tracked because of not nominating 'Sales Persons' into it. Any idea how can I share contact to my sales persons?

public void Execute(IServiceProvider isp)
        {
            IPluginExecutionContext context = (IPluginExecutionContext)isp.GetService(typeof(IPluginExecutionContext));
            ITracingService trace = (ITracingService)isp.GetService(typeof(ITracingService));
            IOrganizationServiceFactory factoy = (IOrganizationServiceFactory)isp.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = factoy.CreateOrganizationService(context.UserId);

            if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
            {
                Entity contect = (Entity)context.InputParameters["Target"];
                Guid guid = ((EntityReference)contect.Attributes["createdby"]).Id;

                var createdReference = new EntityReference("systemuser",guid);

                var grantAccess = new GrantAccessRequest
                {
                    PrincipalAccess = new PrincipalAccess { 
                        AccessMask = AccessRights.ReadAccess,
                        Principal = createdReference
                    },
                    Target = new EntityReference("contect",contect.Id),
                };
                service.Execute(grantAccess);

            }
        }


*This post is locked for comments

I have the same question (0)
  • ThomasN Profile Picture
    3,190 on at
    RE: Share records to only SalesPerson using Plugin in Dynamics CRM

    Hi Mohsin, Thanks for reaching out.

    From the code it seems you are attempting to grant read access to the user who created the contact record.

    Why would you need a plugin to grant access to the user who created the contact record? Or am I missing what you intend to do?

    -Tom

  • Verified answer
    Royal King Profile Picture
    27,686 on at
    RE: Share records to only SalesPerson using Plugin in Dynamics CRM

    I would suggest creating a team and assign a role with read/write privilege on the contact. Add all sales people to this team and share a contact with the team rather than individual people. This way you avoid sharing record with multiple users and it will improve the perforamnce as well.

    You can use below sample code to share a record to team

    var recordRef = new EntityReference(targetEntityName, targetRecordID);

           var teamRef = new EntityReference("team", teamID);

           AccessRights Access_Rights = new AccessRights();

           Access_Rights = AccessRights.None;

           //Read Access          

           if (read_Access == true)

               Access_Rights = AccessRights.ReadAccess;

           //Write Access (or) Read, Write Access        

           if (write_Access == true)

               if (Access_Rights == AccessRights.None)

                   Access_Rights = AccessRights.WriteAccess;

               else

                   Access_Rights = Access_Rights | AccessRights.WriteAccess;

           //Append Access or all or any two accesses        

           if (append_Access == true)

               if (Access_Rights == AccessRights.None)

                   Access_Rights = AccessRights.AppendToAccess | AccessRights.AppendAccess;

               else

                   Access_Rights = Access_Rights | AccessRights.AppendToAccess | AccessRights.AppendAccess;

           var grantAccess = new GrantAccessRequest

           {

               PrincipalAccess = new PrincipalAccess

               {

                   AccessMask = Access_Rights,

                   Principal = teamRef

               },

               Target = recordRef

           };

           // Execute the Request      

           orgService.Execute(grantAccess);

  • ThomasN Profile Picture
    3,190 on at
    RE: Share records to only SalesPerson using Plugin in Dynamics CRM

    Chitra has the right answer here. Using a team would be the best way.

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…

Pallavi Phade – Community Spotlight

We are honored to recognize Pallavi Phade as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Community Member Profile Picture

Community Member 2

#1
UllrSki Profile Picture

UllrSki 2

#3
SC-08081331-0 Profile Picture

SC-08081331-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans