Search code examples
dynamics-crm

Web API alternative for RetrieveSharedPrincipalsAndAccessRequest


I am looking for a way to retrieve sharing permissions on specific entities.

Is there a way to do this via the web api?

I am aware of RetrieveSharedPrincipalsAndAccessRequest but since the rest of my tool relies only on the web api i would like to avoid using Organization Service completely.


Solution

  • This request is available in version 9.0 (Dynamics 365 july update)

    I believe that your are using 8.x version. In such case this is not that simple but also doable. Simply create a custom Action: https://msdn.microsoft.com/en-us/library/dn481600.aspx

    Actions can be used from workflows, plugins and, what is most useful in this scenario, directly called from WebAPI: https://msdn.microsoft.com/en-us/library/mt607600.aspx

    So for example your call would look like that:

    POST [Organization URI]/api/data/v8.2/new_RetrievePrincipalAccessAction HTTP/1.1
    Accept: application/json
    Content-Type: application/json; charset=utf-8
    OData-MaxVersion: 4.0
    OData-Version: 4.0
    
    {
     "ObjectId": 3,
     "ObjectType": "account"
    }
    

    Now, you need to create a plugin and register it in Post-Operation of your action (actions generate a custom message that you can use to register plugins). For example: https://community.dynamics.com/crm/b/magnetismsolutionscrmblog/archive/2017/09/18/how-to-trigger-plugins-on-custom-messages-using-actions-in-dynamics-365

    In your plugin, you can of course call the RetrieveSharedPrincipalsAndAccessRequest using standard IOrganizationService and simply put the result in OutputParameters of an action. Most useful would be JSON string:

    public void Execute(IServiceProvider serviceProvider)
    {
        var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
        //do the logic retrieve what you want
    
        context.OutputParameters["result"] = someResultAsJsonString;
    }
    

    Your JS webAPI call will return

    {
        result: someResultAsJsonString
    }
    

    And you can do whatever you want with this in your JS code :)