web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Dynamics 365 Community / Blogs / heralddyncrm / Hide Relationship Assistant...

Hide Relationship Assistant Tab in CRM form using Javascript & JQuery (Undocumented)

Herald Jeramiah Profile Picture Herald Jeramiah 35

Hi Folks,

Today i will demonstrate how to hide Relationship Assistant which was released in Dynamics CRM Potassium (9.0.2 April Release) based on Security Role using Javascript.

Note: This is Microsoft Undocumented way of hiding Relationship Assistant , So Please be aware of this code before using it in your Project.

Cons: As i am using Timeout concept , It may fails if script loading takes time.

1

Create a Javascript Webresource and add it in form onload.

 

if (typeof (AssistantTab) === "undefined") {
var AssistantTab = {};
}
AssistantTab.OnLoad = {
hideAssistantTab: function () {
try {
//Check if User have System Admin if so hide the tab
var userRoles = AssistantTab.Generic.RetrieveUserRoles();
if (userRoles.indexOf("System Administrator") === -1)
return false;

//To Hide the Assistant Tab
var formElement = $("a[title|='ASSISTANT']");
if (formElement) {
formElement.slideUp();
}
AssistantTab.OnLoad.HideAssistantRuntimeSection();
}
catch (ex) {
console.log("Error occured in AssistantTab.OnLoad.hideAssistantTab , Messgae : " + ex.message)
}
},

HideAssistantRuntimeSection: function () {

var timeOutVar = setTimeout(function () {
var iFrameElement = $("#containerID");
if (iFrameElement) {
iFrameElement.slideUp();
}
else {
AssistantTab.OnLoad.HideAssistantRuntimeSection();
}
}, 700);
}
}

AssistantTab.Generic = {
RetrieveUserRoles: function () {
try {
var apiResult = null;
var userRoles = [];
var fetchXml = ['<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true"><entity name="role"><attribute name="name" /><attribute name="businessunitid" /><attribute name="roleid" /> <order attribute="name" descending="false" /> <link-entity name="systemuserroles" from="roleid" to="roleid" visible="false" intersect="true"> <link-entity name="systemuser" from="systemuserid" to="systemuserid" alias="ae"> <filter type="and"> <condition attribute="systemuserid" operator="eq-userid" /> </filter> </link-entity> </link-entity> </entity></fetch>']
var encodeFetchXml = encodeURIComponent(fetchXml);
var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.0/roles?fetchXml=" + encodeFetchXml, false);
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.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
req.onreadystatechange = function () {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
apiResult = JSON.parse(this.response);
}
}
};
req.send();

//Retrieve UserRoles from JSON
if (apiResult && apiResult.value.length > 0) {
for (var i = 0; i < apiResult.value.length; i++) {
userRoles.push(apiResult.value[i]["name"]);
}
}
return userRoles;
}
catch (ex) {
console.log("Error occured in AssistantTab.Generic.RetrieveUserRoles , Messgae : " + ex.message)
}
}
}

Open Form Editor ->  Form Properties -> Add hideAssistantTab Function OnLoad -> Save and Publish.

Once the function enabled , it will Check whether the Logged-in User have System Admin Role If So then hide the Relationship Assistant Section from the form as explained below.

 hideAssistantTab: function () {
        try {
           //Check if User have System Admin if so hide the tab
            var userRoles = AssistantTab.Generic.RetrieveUserRoles();
            if (userRoles.indexOf("System Administrator") == -1)
                return false;

            //To Hide the Assistant Tab
            var formElement = $("a[title|='ASSISTANT']");
            if (formElement) {
                formElement.slideUp();
            }
            AssistantTab.OnLoad.HideAssistantRuntimeSection();
        }
        catch (ex) {
            console.log("Error occured in AssistantTab.OnLoad.hideAssistantTab , Messgae : " + ex.message)
        }
    }
RetrieveUserRoles: function () {
        try {
            var apiResult = null;
            var userRoles = [];
            var fetchXml = ['<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true"><entity name="role"><attribute name="name" /><attribute name="businessunitid" /><attribute name="roleid" />    <order attribute="name" descending="false" />    <link-entity name="systemuserroles" from="roleid" to="roleid" visible="false" intersect="true">      <link-entity name="systemuser" from="systemuserid" to="systemuserid" alias="ae">        <filter type="and">          <condition attribute="systemuserid" operator="eq-userid" />        </filter>      </link-entity>    </link-entity>  </entity></fetch>']
            var encodeFetchXml = encodeURIComponent(fetchXml);            
            var req = new XMLHttpRequest();
            req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.0/roles?fetchXml=" + encodeFetchXml, false);
            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.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
            req.onreadystatechange = function () {
                if (this.readyState === 4) {
                    req.onreadystatechange = null;
                    if (this.status === 200) {
                        apiResult = JSON.parse(this.response);                       
                    }
                }
            };
            req.send();

        if (apiResult && apiResult.value.length > 0) {
                for (var i = 0; i < apiResult.value.length; i++) {
                    userRoles.push(apiResult.value[i]["name"]);
                }
            }
            return userRoles;
        }
        catch (ex) {
            console.log("Error occured in AssistantTab.Generic.RetrieveUserRoles , Messgae : " + ex.message)
        }
    }

The AssistantTab.Generic.RetrieveUserRoles retrieves currently logged in users Roles using Web API and Check if the user have System Admin role, if so then search for the Relationship Assistant Section HTML element using JQuery and remove the element from the form.
 //Hide Relationship Assistant Cards
    HideAssistantRuntimeSection: function () {       
        var timeOutVar = setTimeout(function () {
            var iFrameElement = $("#containerID");
            if (iFrameElement) {
                iFrameElement.slideUp();
            }
            else {
                AssistantTab.OnLoad.HideAssistantRuntimeSection();
            }
        }, 700);
    }
 

Relationship Assistant Cards appears once the form scripts fully loaded at run-time. So i am using Timeout function in JavaScript to hide the Relationship Assistant Cards as shown above.

I have declared 700 milliseconds time interval , so that the HideAssistantRuntimeSection will triggers at every time interval .

It will check whether the Relationship Assistant Card have been loaded , If loaded then it will remove the Html Element using custom JavaScript code(Unsupported Way) else it will trigger the function again until card get loaded.

Hope it's Helpful. Happy Coding :) 

Comments

*This post is locked for comments