CRM 2011 JavaScript User Object RRS feed

  • Question

  • I wondering if anyone is creating a JavaScript object to store user credentials after an initial process to load a user's role.  Getting a list of user's role's to check against for various security level access to buttons, fields, data, etc. requires individual calls to the CRM database.  It seems that the overhead is a bit much on every postback.  So I am wondering if anyone has tried to create something in JavaScript to better handle this... or maybe I am approaching this altogether incorrectly.

    As an example, consider that if we have 3 levels of access... user, manager, director.  Each time a user hits CRM we have to figure out which type of user they are.  So we then request from CRM the user's roles, which are returned as GUIDs.  We then translate those into descriptions with a second hit to the CRM database, as the GUID is unique to the system and we cannot use the GUID in our logic.  We get the string description and we can then use it to determine if the user has what level of access.  Then we turn off and on the various create, qualify, and other buttons.  Now in the ribbon I did not see anyway to wire in a display rule tied to the security role name.  I had to add a JavaScript rule and use JavaScript to handle this.  And hense the problem...

    Here's what each save, refrech, post-back, etc. has to do. Is there a better way? Can I load it once like I would in ASPX and hold the value?  I am not a JavaScript expert so I don't know how to correctly approach this in JavaScript. Here's the steps... 

    1. Pull the user's roles.
    2. Loop through the roles to get the GUIDs.
    3. For each GUID, call out to CRM and translate the GUID to the string description of the role.
    4. Check to see if the role matches one of our three roles. Checks the highest access level/role first and then goes to the lowest so that if a match is found, we can exit the process as early as possible.  For example, I check if a user is in the admin role first, as there is no need to continue and check for any others.
    5. When a match is found we drop into JavaScript logic to set the button's visibility and/or enabled the button(s).

    This seems a bit much for every postback.  Is there a better way?  If I was doing this without session variables and in ASP.Net, I could create a user class to hold the user access level and use it to check on each postback.  But in JavaScript, I am not so sure how best to do this.  I've even considered use a table, a cookie, etc. to just store the user's access level.  Then just have a short timeout value... like an hour to make sure we can change their access level as needed.

    I realize I need to beef up on my JavaScript and I am doing so.  I have only used JavaScript for web pages events going back to the late 90's and never really anything more than that.  So I might be missing something very simple here.


    Jon Gregory Rothlander

    Monday, April 7, 2014 3:48 PM

All replies

  • Hello,

    To get user security roels you don't need to call crm webservice, there is a direct method available for this

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

    and then you need to make call to crm webservice that you are doing currently to get description, and you can use your functions in button display/enable rule to return true or false based on your logic and crm will take case of hiding enabling button for you.

    Check this: http://dynamicscrm2011.wordpress.com/2011/05/19/ribbon-customization-part-11enabledisable-ribbon-button-based-on-security-role/


    Our Website | Our Blog | Follow US | My Facebook Page | Microsoft Dynamics CRM 2011 Application Design
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Tuesday, April 8, 2014 12:44 AM
  • Thanks for your reply. 

    I looked at the link that you provided and that is basically what I am doing.  But this looks like a lot of work for the code to go through on every postback. 

    My users have 2 or 3 roles and the sytem has 37 possible roles.  So say if my user has 3 roles and there are three levels of a user (user, manager, admin) to run through the logic detailed in that link, it will have to run through the loop up to 111 (3x37) times if no match is found, or less if a match is found.  This will need to be reran on every postback.  All I need to save is the user level (user, manager, admin), which translates to (1, 2, or 3).  In a traditional .Net app you can do things like create a session variable, maybe you'd call it Session["AccessLevel"] and set it when the user initially hits the site.  Then on subsequent postbacks you simply check the session variable.  But in CRM it seems that you have to just take the hit of the 111 calls to the web service on every postback.  That seems like a performance concern. 

    If I could just save off that 1 byte of information, I could save 111 calls to the CRM web service.  But I don't know how to correctly do this in JavaScript and CRM.  I could create an entity just for this or a db table.  Then save the 1 character... it would reduce the calls from 111 down to 1.  But something like a session variable or cookie seems like the best approach. 

    Any thoughts? 

    Jon Gregory Rothlander

    Tuesday, April 8, 2014 2:17 PM