Blog, Development, Howto

How to share record with user using WebApi

You can use following code to share record (account in this case) with user (but you can share it with team as well) using JavaScript and WebApi without any additional development:

var target = {
		"accountid": "ACAAB842-21C7-E811-A96F-000D3A16A41E",
		//put <other record type>id and Guid of record to share here
		"@odata.type": "Microsoft.Dynamics.CRM.account"
		//replace account with other record type

var principalAccess = {
		"Principal": {
			"systemuserid": "d272654b-57f5-4564-8d0b-36d0d4c426c4",
			//put teamid here and Guid of team if you want to share with team
			"@odata.type": "Microsoft.Dynamics.CRM.systemuser"
			//put team instead of systemuser if you want to share with team
		"AccessMask": "ReadAccess, WriteAccess"
		//full list of privileges is "ReadAccess, WriteAccess, AppendAccess, AppendToAccess, CreateAccess, DeleteAccess, ShareAccess, AssignAccess"

var parameters = {
    "Target": target,
    "PrincipalAccess": principalAccess

var context;

if (typeof GetGlobalContext === "function") {
    context = GetGlobalContext();
} else {
    context = Xrm.Page.context;

var req = new XMLHttpRequest();"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


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.