Show/Hide Tab or Section Based on Login User(Security Role)

Show Hide Tab or Section Based on Login User(Security Role)

Create WebResource And place the Below code then cal the function where you want to Need the Functionality

function ShowHideDispatchSection() {

    //Example Role: System Administrator

    if (UserHasRole(“Role”) || UserHasRole(“Role “)) {

        //  Tab Name or Guid of it

        Xrm.Page.ui.tabs.get(“1”).sections.get(“5”).setVisible(true);

}

else {

        Xrm.Page.ui.tabs.get(“1”).sections.get(“5”).setVisible(false);

    }

}

function UserHasRole(roleName) {

    var serverUrl = Xrm.Page.context.getServerUrl();

    var oDataEndpointUrl = serverUrl + “/XRMServices/2011/OrganizationData.svc/”;

    var org = Xrm.Page.context.getOrgUniqueName()

    var oDataEndpointUrl = “/” + org + “/XRMServices/2011/OrganizationData.svc/”;

    oDataEndpointUrl += “RoleSet?$top=1&$filter=Name eq ‘” + roleName + “‘”;

    var service = GetRequestObject();

    if (service != null) {

service.open(“GET”, oDataEndpointUrl, false);

service.setRequestHeader(“X-Requested-Width”, “XMLHttpRequest”);

service.setRequestHeader(“Accept”, “application/json, text/javascript, */*”);

        service.send(null);

        var requestResults = eval(‘(‘ + service.responseText + ‘)’).d;

        if (requestResults != null && requestResults.results.length == 1) {

            var role = requestResults.results[0];

            var id = role.RoleId;

            var currentUserRoles = Xrm.Page.context.getUserRoles();

            for (var i = 0; i < currentUserRoles.length; i++) {

                var userRole = currentUserRoles[i];

                if (GuidsAreEqual(userRole, id))

{
                    return true;
                }

           }

      }

    }

    return false;

}

function GetRequestObject()

{

    if (window.XMLHttpRequest)

{        return new window.XMLHttpRequest;

    }

else {

        try {

            return new ActiveXObject(“MSXML2.XMLHTTP.3.0”);

        }

catch (ex) {

            return null;

        }

    }

}

function GuidsAreEqual(guid1, guid2) {

    var isEqual = false;

    if (guid1 == null || guid2 == null) {

        isEqual = false;

    }

else {

        isEqual = guid1.replace(/[{}]/g, “”).toLowerCase() == guid2.replace(/[{}]/g, “”).toLowerCase();

}

    return isEqual;

}

 

Advertisements

4 thoughts on “Show/Hide Tab or Section Based on Login User(Security Role)

  1. Hi, I am getting error saying “There is an error with this fields customized event” Field:Window,
    Event:Onload,
    Error SHowHideDispatchsection is undefined.

    Could anybody help me solve this issue.

  2. Hi I am new to CRM and java script coding. Since I was getting error I just commented out
    the visible/invisible to an alert. Still the same issue.

    Below code I put in as a web resource file and named is aam_hideunhide. In the Case form properties I added aam_hideunhide in form library and in event library I added ShowhideDispatchsection. Saved the form and published it.

    function ShowHideDispatchSection() {

    if UserHasRole(“IT Support”) == true {

    // Tab Name or Guid of it

    //Xrm.Page.ui.tabs.get(“1″).sections.get(“3″).setVisible(true);
    alert(“true”);

    }

    else {

    //Xrm.Page.ui.tabs.get(“1″).sections.get(“3″).setVisible(false);
    alert(“false”);
    }

    }

    function UserHasRole(roleName) {

    var serverUrl = Xrm.Page.context.getServerUrl();

    var oDataEndpointUrl = serverUrl + “/XRMServices/2011/OrganizationData.svc/”;

    var org = Xrm.Page.context.getOrgUniqueName()

    var oDataEndpointUrl = “/” + org + “/XRMServices/2011/OrganizationData.svc/”;

    oDataEndpointUrl += “RoleSet?$top=1&$filter=Name eq ‘” + roleName + “‘”;

    var service = GetRequestObject();

    if (service != null) {

    service.open(“GET”, oDataEndpointUrl, false);

    service.setRequestHeader(“X-Requested-Width”, “XMLHttpRequest”);

    service.setRequestHeader(“Accept”, “application/json, text/javascript, */*”);

    service.send(null);

    var requestResults = eval(‘(‘ + service.responseText + ‘)’).d;

    if (requestResults != null && requestResults.results.length == 1) {

    var role = requestResults.results[0];

    var id = role.RoleId;

    var currentUserRoles = Xrm.Page.context.getUserRoles();

    for (var i = 0; i < currentUserRoles.length; i++) {

    var userRole = currentUserRoles[i];

    if (GuidsAreEqual(userRole, id))

    {
    return true;
    }

    }

    }

    }

    return false;

    }

    function GetRequestObject()

    {

    if (window.XMLHttpRequest)

    { return new window.XMLHttpRequest;

    }

    else {

    try {

    return new ActiveXObject(“MSXML2.XMLHTTP.3.0″);

    }

    catch (ex) {

    return null;

    }

    }

    }

    function GuidsAreEqual(guid1, guid2) {

    var isEqual = false;

    if (guid1 == null || guid2 == null) {

    isEqual = false;

    }

    else {

    isEqual = guid1.replace(/[{}]/g, “”).toLowerCase() == guid2.replace(/[{}]/g, “”).toLowerCase();

    }

    return isEqual;

    }

    • Hi ,

      did you replace special characters in the Code?

      If you directly copy it from the web to your local those special characters are looking diff format . Please change it and try again.

      Regards,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s