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 :
Microsoft Dynamics CRM (Archived)

CRM for Phone app - how to disable field according to Security role

(0) ShareShare
ReportReport
Posted on by

Hello,

We are on CRM 2015 update 1 online and are using the new CRM for Phone app.

On our appointment form, we disable/enable some fields according to the security role of the user by using Javascript getUserRoles and UserHasRole. This works fine in the browser, but does not seem to work in the Phone app.

How can we do the same in the Phone app?

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Nele Depraetere Profile Picture
    on at
    RE: CRM for Phone app - how to disable field according to Security role

    Our partner was able to fix this issue.

    Function _retrieveFirstOrDefault called function _getOdataServerUrl

    ---

        var _getOdataServerUrl = function () {
            return ProdwareToolKit.COMMON.GetBaseUrl() + "/XRMServices/2011/OrganizationData.svc/";
        };

    ---

        var _getBaseUrl = function () {

            var hostname = document.location.host;

            if (hostname.length > 13) {
                if (hostname.substring(hostname.length - 13).toUpperCase() == ".dynamics.com".toUpperCase()) {
                    //CRM Online
                    return document.location.protocol + "//" + document.location.host + "/"
                }
                else {
                    //CRM On Premise
                    return document.location.protocol + "//" + document.location.host + "/" + Xrm.Page.context.getOrgUniqueName();
                }
            }
            else
                return document.location.protocol + "//" + document.location.host + "/" + Xrm.Page.context.getOrgUniqueName();

        };

    ---

    was changed to

    ---

        var _getBaseUrl = function () {

            return Xrm.Page.context.getClientUrl();

        };

    ---

    and now it works

  • Nele Depraetere Profile Picture
    on at
    RE: CRM for Phone app - how to disable field according to Security role

    Hello,

    The last alert I get is 'In _retrieveFirstOrDefault - service = ...', so this would mean that service is null.

    I never get message 'Planningstatus enable' or 'Planningstatus disable'

    The last message I get is an error 'There was an error with field's customized event. Window. load. A network error occurred'

        Form_onload: function (XrmContext) {
            //Xrm.Utility.alertDialog("Start onload");
            if (ProdwareToolKit.COMMON.UserHasRole("System Administrator")
                || ProdwareToolKit.COMMON.UserHasRole("Project manager")
                || ProdwareToolKit.COMMON.UserHasRole("Operations manager")
                || ProdwareToolKit.COMMON.UserHasRole("Product manager")) {
             Xrm.Utility.alertDialog("Planningstatus enable");
                ProdwareToolKit.FORMSCRIPTING.EnableControl("tr_planningstatus", true);
            } else {
       Xrm.Utility.alertDialog("Planningstatus disable");
                ProdwareToolKit.FORMSCRIPTING.DisableControl("tr_planningstatus", true);
            }

       var _userHasRole = function (RoleName) {

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

    Xrm.Utility.alertDialog("In _userHasRole: currentUserRoles = " + currentUserRoles);

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

               var userRole = currentUserRoles[i];

    Xrm.Utility.alertDialog("userRole = " + userRole);

               var retrievedRoles = ProdwareToolKit.ODATA.RetrieveFirstOrDefault("/RoleSet?$filter=RoleId eq guid'" + userRole + "'");

    Xrm.Utility.alertDialog("retrievedRoles = " + retrievedRoles);

               if (retrievedRoles != null) {

                   if (retrievedRoles.Name.toUpperCase() == RoleName.toUpperCase())

                       return true;

               }

           }

           return false;

       };

       var _retrieveFirstOrDefault = function (Query) {

           if (Query == null)

               return null;

           _loadJSONAndJQueryLibrary();

           var oDataEndpointUrl = _getOdataServerUrl() + Query;

           var service = _getRequestObject();

    Xrm.Utility.alertDialog("In _retrieveFirstOrDefault - service = " + service);

           if (service != null) {

               service.open("GET", oDataEndpointUrl, false);

               service.setRequestHeader("Accept", "application/json");

               service.setRequestHeader("Content-Type", "application/json; charset=utf-8");

               service.send(null);

    Xrm.Utility.alertDialog("service.status = " + service.status);

               if (service.status != 200) {

                   _errorHandler(service);

                   return null;

               }

               var retrievedData = JSON.parse(service.responseText, dateReviver).d;

    Xrm.Utility.alertDialog("retrievedData = " + retrievedData);

               if (retrievedData != null && retrievedData.results.length != 0) {

                   return retrievedData.results[0];

               }

               else {

                   return null;

               }

           }

           return null;

       };

  • Nele Depraetere Profile Picture
    on at
    RE: CRM for Phone app - how to disable field according to Security role

    Hello,

    I added some comment in the Onload function, as well as in the UserHasRole function, which is in an other library

    First I get alert 4, then 5, then 1 and then the form is loaded

  • Nele Depraetere Profile Picture
    on at
    RE: CRM for Phone app - how to disable field according to Security role

    Yes it runs in onLoad. I don't know if it doesn't run or if the function doesn't return any roles.

    The field that should be enabled because the user has the Project manager role is disabled when I create a new appointment on the Phone, and also when I open an existing appointment on the Phone.

  • Nele Depraetere Profile Picture
    on at
    RE: CRM for Phone app - how to disable field according to Security role

    Hello,

    this code was written by our partner Prodware

    if (ProdwareToolKit.COMMON.UserHasRole("System Administrator")

               || ProdwareToolKit.COMMON.UserHasRole("Project manager")

               || ProdwareToolKit.COMMON.UserHasRole("Operations manager")

               || ProdwareToolKit.COMMON.UserHasRole("Product manager"))

    ProdwareToolKit:

    return {

           UserHasRole: _userHasRole,

       var _userHasRole = function (RoleName) {

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

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

               var userRole = currentUserRoles[i];

               var retrievedRoles = ProdwareToolKit.ODATA.RetrieveFirstOrDefault("/RoleSet?$filter=RoleId eq guid'" + userRole + "'");

               if (retrievedRoles != null) {

                   if (retrievedRoles.Name.toUpperCase() == RoleName.toUpperCase())

                       return true;

               }

           }

           return false;

       };

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Aric Levin - MVP Profile Picture

Aric Levin - MVP 2 Moderator

#1
HR-09070029-0 Profile Picture

HR-09070029-0 2

#3
MA-04060624-0 Profile Picture

MA-04060624-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans