Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Hide a Field Section on Lead Form on the Basis of User Role

Posted on by Microsoft Employee

I am using Dynamics 365 online version. I want to implement a scenario in which i can hide/show a section based upon User Role currently login.

I was looking for the XRMToolkit package to achieve this, but its not compatible with CRM 2016.

Please give me a solution for this.

*This post is locked for comments

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Hide a Field Section on Lead Form on the Basis of User Role

    I have Used These 3 Functions to Achieve This using Javascript :

    function ShowHideSection() {

       if (UserHasRole("Sales Person")) {

           //  Tab Name or Guid of it

           if (Xrm.Page.ui.tabs.get("Account Research") != null) {

               Xrm.Page.ui.tabs.get("Account Research").setVisible(true);

           }

           // tab_4_section_3

           if (Xrm.Page.ui.tabs.get("LeadGen Information") != null) {

               Xrm.Page.ui.tabs.get("LeadGen Information").setVisible(false);

           }

       }

       else if (UserHasRole("Sales Manager")) {

           //  Tab Name or Guid of it

           if (Xrm.Page.ui.tabs.get("Account Research") != null) {

               Xrm.Page.ui.tabs.get("Account Research").setVisible(false);

           }

           // tab_4_section_3

           if (Xrm.Page.ui.tabs.get("LeadGen Information") != null) {

               Xrm.Page.ui.tabs.get("LeadGen Information").setVisible(true);

           }

       }

       else {

           //Xrm.Page.ui.tabs.get("tab_6_section_2").sections.get("5").setVisible(false);

           if (Xrm.Page.ui.tabs.get("Account Research") != null) {

               Xrm.Page.ui.tabs.get("Account Research").setVisible(false);

           }

       }

    }

    function UserHasRole(roleName) {

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

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

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

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

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

       var oDataEndpointUrl = "/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 retrieved = JSON.parse(service.responseText).d;

           var role = retrieved.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;

               }

           }

           //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;

    }

  • Suggested answer
    Wouter Madou Profile Picture
    Wouter Madou 3,392 on at
    RE: Hide a Field Section on Lead Form on the Basis of User Role

    Just create your form and duplicate it.

    Delete the fields/sections that you do not want them to see.

    Set your first form as the fallback form and the second one as the default form for that security role.

  • Suggested answer
    Aric Levin Profile Picture
    Aric Levin 30,188 on at
    RE: Hide a Field Section on Lead Form on the Basis of User Role

    You can do this using JavaScript.

    The Xrm.Page.context has a function called getUserRoles, which will return an array of Guids of the different roles that a user belongs to.

    You can also use the webapi in Dynamics 365 to get the roles for a particular user, using the following API calls:

    // Will give you list of roles

    api/data/v8.2/systemuserrolescollection?$select=roleid&$filter=systemuserid eq "+userId

    // Will give you names of roles

    /api/data/v8.2/roles("+roleId+")?$select=name

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans