Search code examples
entitydynamics-crm-2011dynamics-crmrecordsharing

MS Dynamics CRM. Get users who current record shared with


I have a entity record which is shared with or more users. I would like to unshare this record when Deactivate it. I want to do that in Plugin. But I can't understand how to get all users from sharing list who have access to this record. How to do that?

Here is my code snippet:

    protected void ExecutePostPersonSetStateDynamicEntity(LocalPluginContext localContext)
    {
        if (localContext == null)
        {
            throw new ArgumentNullException("localContext");
        }

        var context = localContext.PluginExecutionContext;

        var targetEntity = (Entity)context.InputParameters["EntityMoniker"];
        var state = (OptionSetValue)context.InputParameters["State"];
        var columns = new ColumnSet(new[] { "statecode" });

        var retrivedEntity = localContext.OrganizationService.Retrieve(targetEntity.LogicalName, targetEntity.Id, columns);

        if (state.Value == 1)
        {
            RevokeAccessRequest revokeRequest = new RevokeAccessRequest()
            {
                Target = new EntityReference(personEntity.LogicalName, personEntity.Id),
                Revokee = new EntityReference(neededEntity.LogicalName, needed.Id)
            };

            // Execute the request.
        }
    }

As you can see, I need an entity "neededEntity", I don't know how to get it from "targetEntity" or "retrievedEntity".


Solution

  • You need to use a RetrieveSharedPrincipalsAndAccessRequest

    http://msdn.microsoft.com/en-us/library/microsoft.crm.sdk.messages.retrievesharedprincipalsandaccessrequest.aspx

    You can start from the included example, basically inside the foreach you call your RevokeAcessRequest