Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

Sharing Record Programatically

Posted on by 185

Hi,

I have an entity called A . All users have user level access on the entity.So, users can access the records only those are created by them.

My requirement is , in some scenarios , when record is created by an user , it has to be shared (give READ access) with some other users based on some condition.

Below is my approach :

1) Register a plugin on create of record.In the plugin, use the PrinciapalAccess() method to share the record


Is there any other approach ? Anything can be done using Access Teams programatically ? Thanks !!

*This post is locked for comments

  • Suggested answer
    Sreevalli Profile Picture
    Sreevalli 3,256 on at
    RE: Sharing Record Programatically

    Hi,

    we need to consider below things in this case.

    1. Make sure the users to whom you are trying to share should have read privilege(at least at user level)

    2. Don't depend on Target, try to retrieve the target record from context. - 

         Entity target = service.Retrieve(context.PrimaryEntityName, context.PrimaryEntityId, new ColumnSet("your fields"));

    3. Validate the user context and the user role that must have read privilege on your target record.

    4. Plugin context depth. to see if there are any cross scripting that breaking your logic.

  • Suggested answer
    Kokulan Profile Picture
    Kokulan 18,048 on at
    RE: Sharing Record Programatically

    Hi

    Please make sure the Entity Reference you use in the grant request has the correct logical name

    If you still have the same issue, i would try the following

    Move your sharing code to a Custom Workflow Activity (CWA) and call this on a background workflow and share and see if that works.

    About the user ids, please see below

    Initiatinguserid:

    This is the user who performs the operation that triggers the plugin. If USER-A creates a record and a plugin is triggered on record create then the Initiatinguserid is USER-A.

    Userid:

    This contains the "run in user's context" users id that is set as a part of plugin step creation. If the value is not set, then it equivalent to Initiatinguserid

    community.dynamics.com/.../ms-crm-plugin-ipluginexecutioncontext-initiatinguserid-and-userid-plugintip2

  • Suggested answer
    Dynamics365 Rocker Profile Picture
    Dynamics365 Rocker 7,755 on at
    RE: Sharing Record Programatically

    Yes, You can do it by access team.

  • Dynamics 365 CRM Profile Picture
    Dynamics 365 CRM 185 on at
    RE: Sharing Record Programatically

    Thank you Kokulan for the response.But, for time being I followed first approach.But still I am facing below issue

    I have a plugin registered (post-operation,synchronous) on create of a custom entity. In the plugin , I am trying to share (using some custom code) the record with another user. My problem is

    The code is throwing an exception as "the record with guid = {XXXXX-XXXXX-XXXX} you are trying to share doesn't exist".

    This is strange. Because, I registered plugin on post-operation. That means , after the record got created in DB, the plugin triggers.

    Any idea why it's throwing exception ?

    Also , another thing that I observed while debugging is , context.UserId and context.InitiatingUserId are showing same values through I registered plugin under different user.Here both are showing my Id i.e who is performing create record. Any idea on this ?  

  • Suggested answer
    Kokulan Profile Picture
    Kokulan 18,048 on at
    RE: Sharing Record Programatically

    Hi

    Sharing using plugin would definitely work and, if you want to have a look at a full example please follow the link below

    community.dynamics.com/.../crm-2011-2013-share-and-un-share-records-programmatically-c

    But I would definitely recommend using an access team template-based approach. When you create the template you can set the permission to be just Read

    Please refer to the link below to know the difference between Share and Access Team based approach

    community.dynamics.com/.../919501

    Please follow the link below to see how you can programmatically add or remove users to an access team of a record

    community.dynamics.com/.../add-remove-users-to-access-team-dynamically

    If you would like to know more about access team, please refer to the links below

    docs.microsoft.com/.../use-access-teams-owner-teams-collaborate-share-information

  • Sanket87 Profile Picture
    Sanket87 160 on at

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,240 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans