This is a 3rd post of this series of blog posts, Previously we have described CRUD operations using SDK.SOAP.Js and Bulk Operations using SDK.SOAP in Javascript. In this blog post we would like to share details of  how can we retrieve user privileges and entity metadata using SDK.SOAP.JS.  The following code snippets will show the details of these messages.

Retrieve Privileges

When a custom grid is required besides other specifications we also need to inherit Microsoft Dynamics CRM security model. In order to inherit security model we need to get privileges of current user for the specific entity. In the following code snippet we will retrieve the current user privileges for an entity.

  1. Include the “RetrievePrincipalAccess.js” as web resource in the desired solution. Include the web resource in HTML document.
  2. Instantiate Entity References of the user whose privileges need to be retrieved using

new Sdk.EntityReference()

First parameter will be the entity logical name (“systemuser”) and second parameter will be the GUID of user.

3. Instantiate Entity References of the record whose privileges need to be retrieved using

new Sdk.EntityReference()

First parameter will be the entity logical name and second parameter will be the GUID of entity.

4. Now instantiate the request for retrieving privileges from CRM using

new Sdk.RetrievePrincipalAccessRequest();

First parameter will be record entity reference and second parameter will be user entity reference.

5. Now execute the PrincipleAccessRequest() in Async mode. It will return all privileges with their Boolean value.

Complete function call to retrieve user privileges for an entity

function Retrieveprivileges() {
//Get User GUID, Record GUID and Entity NAME
var userId = parent.Xrm.Page.context.getUserId();
var recordId =;
var entityName =;

//Instantiate User Entity Reference
var userEntityReference = new Sdk.EntityReference("systemuser", userId);
//Instantiate User Entity Reference
var recordEntityReference = new Sdk.EntityReference(entityName, recordId);
//Instantiate Organization Serivce Request
var principalAccessRequest = new Sdk.RetrievePrincipalAccessRequest(recordEntityReference, userEntityReference);

//Execute Request in Async Mode
function (res) {

//Get Response Object
if (res.getAccessRights().AppendAccess == true) {
console.log("AppendAccess is granted");

if (res.getAccessRights().AppendToAccess == true) {
console.log("AppendToAccess is granted");

if (res.getAccessRights().AssignAccess == true) {
console.log("AssignAccess is granted");

if (res.getAccessRights().CreateAccess == true) {
console.log("CreateAccess is granted");

if (res.getAccessRights().DeleteAccess == true) {
console.log("DeleteAccess is granted");

if (res.getAccessRights().ReadAccess == true) {
console.log("ReadAccess is granted");

if (res.getAccessRights().ShareAccess == true) {
console.log("ShareAccess is granted");

if (res.getAccessRights().WriteAccess == true) {
console.log("WriteAccess is granted");
//Call back function for Error Handling
function (e) {

Now on the basis of available user privileges we can easily implement the Microsoft Dynamics CRM security model in our customs grids.


Retrieve Entity Metadata

For normal operations of Microsoft Dynamics CRM we do not need entity metadata. But it is required when we need to retrieve formatted values. In the following code snippet we will retrieve account entity metadata.

  1. Include the “RetrieveMetadataChanges.js” as web resource in the desired solution. Include the web resource in HTML document.
  2. We need to use namespaces in this code snippet so first we will create their alias so that we do not have to type whole namespace again and again.
  3. Instantiate entity filter and add condition to it to return only account entity meta data.
  4. Instantiate MetadataPropertiesExpression() using.
new mdq.MetadataPropertiesExpression(); 

The first parameter will be false because we do not need all properties and second parameter will be array of properties that need to be retrieved.

5. Instantiate MetadataFilterExpression() using

 new mdq.MetadataFilterExpression(); 

We have to pass the logical operator type as a parameter.

6.Instantiate MetadataPropertiesExpression() using

 new mdq.MetadataPropertiesExpression(); 

to retrieve attribute type and schema name.

7. Instantiate query using

 new mdq.EntityQueryExpression();

first parameter will be MetadataFilterExprssion(), second parameter will be MetadataPropertiesExpression() and third parameter will be AttributeQueryExpression().

8. Execute query in Async mode. The query will return all the metadata of the requested entity attribute properties. In our case it will be account entity attributes properties.

Complete function call to retrieve entity metadata

function RetrieveMetadata() {

//Create Name Space Alais
var mdq = Sdk.Mdq;
var semp = Sdk.Mdq.SearchableEntityMetadataProperties;
var samp = Sdk.Mdq.SearchableAttributeMetadataProperties;
var srmp = Sdk.Mdq.SearchableRelationshipMetadataProperties
var emp = Sdk.Mdq.EntityMetadataProperties;
var amp = Sdk.Mdq.AttributeMetadataProperties;
var rmp = Sdk.Mdq.RelationshipMetadataProperties;
var ve = Sdk.Mdq.ValueEnums;

//Instantiate entity filter
var entityFilter = new mdq.MetadataFilterExpression(mdq.LogicalOperator.And);
entityFilter.addCondition(semp.SchemaName, mdq.MetadataConditionOperator.Equals, "Account");

//Instantiate and specify Entity properties to retrive
var entityProperties = new mdq.MetadataPropertiesExpression(false, [emp.Attributes, emp.PrimaryIdAttribute]);

//Instantiate Attibutes Filter to retrieve specific attributes
var attributeFilter = new mdq.MetadataFilterExpression(mdq.LogicalOperator.And);
attributeFilter.addCondition(samp.AttributeType, mdq.MetadataConditionOperator.NotEquals, ve.AttributeTypeCode.Virtual);

//Specify attributes to retrieve
var attributeProperties = new mdq.MetadataPropertiesExpression(false, [amp.AttributeType, amp.SchemaName]);

//Make the final query for Metadata Retieval
var query = new mdq.EntityQueryExpression(
new mdq.AttributeQueryExpression(attributeFilter, attributeProperties));

//Execute Query in Async Mode
Sdk.Async.execute(new Sdk.RetrieveMetadataChangesRequest(query),
function (res) {
//Get Organization Response
var entities = res.getEntityMetadata()[0]

for (var i = 0; i < entities.Attributes.length; i++) {
console.log(entities.Attributes[i].AttributeType + ":" + entities.Attributes[i].LogicalName);
//Call back function for Error Handling
function (e) {


Now we have retrieved the entity metadata we can now take next steps to use this retrieved metadata.