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)

Sharing Record Programatically

(0) ShareShare
ReportReport
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

I have the same question (0)
  • Sanket87 Profile Picture
    160 on at
  • Suggested answer
    Kokulan Profile Picture
    18,054 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

  • Dynamics 365 CRM Profile Picture
    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
    Dynamics365 Rocker Profile Picture
    7,755 on at
    RE: Sharing Record Programatically

    Yes, You can do it by access team.

  • Suggested answer
    Kokulan Profile Picture
    18,054 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
    Sreevalli Profile Picture
    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.

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…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
HR-09070029-0 Profile Picture

HR-09070029-0 2

#2
ED-30091530-0 Profile Picture

ED-30091530-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans