Grant Access using Web API for External Application

We recently had a Requirement to share a record using Web API.

Situation :  Front End Application is developed in HTML and is integrated with CRM using CRM exposed Web API.

As every entity expose API. Performing CRUD become simple using those API.
But what about sharing a record with teams or user?

One of the challenge we faced is of sharing record.
Rest-Builder dint work here. it is not able to develop a request for sharing record.

Following Request can be embedded in JavaScript code which will help to perform sharing a record to specific team/user.

Request method POST
Request URL :  https://[orgname].com/api/data/v9.0/GrantAccess
This GrantAccess message helps us to share record in CRM.
Request Body :

Below is JSON request body.
We are sharing opportunity record with Specific user of organization.

var parameters = {
   "Target":{
      "opportunityid":"4A9C6446-C856-E911-A830-000D3AA058CB", // Opp ID
      "@odata.type":"Microsoft.Dynamics.CRM.opportunity"
   },
   "PrincipalAccess":{
      "Principal":{
         "systemuserid":"16CB3189-1852-E911-A830-000D3AA058CB", // User ID
         //put teamid here and Guid of team if you want to share with team
         "@odata.type":"Microsoft.Dynamics.CRM.systemuser"
      },
      "AccessMask":"ReadAccess, WriteAccess"  
    //full list of privileges is "ReadAccess, WriteAccess, AppendAccess, AppendToAccess,  CreateAccess, DeleteAccess, ShareAccess, AssignAccess"
   }
};

Code to Execute above Request by passing above JSON

var req = new XMLHttpRequest();
req.open("POST", context.getClientUrl() + "/api/data/v9.0/GrantAccess", true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.onreadystatechange = function () {
    if (this.readyState === 4) {
        req.onreadystatechange = null;
        if (this.status === 204) {
            //Success - No Return Data - Do Something
        } else {
            var errorText = this.responseText;
            //Error and errorText variable contains an error - do something with it
        }
    }
};
req.send(JSON.stringify(parameters));
This will share your Record to specified user.

This code can be tested in Postman or can be added in your external application.

Hope it helps 🙂

More About This Author

Friyank Parikh

Author: Friyank Parikh

Share This Post On
Share via
Copy link
Powered by Social Snap