Hide and show fields based on the logged in user

There have been a lot of requests on how to hide and show fields since my earlier postings regarding hiding and showing fields and rows. John Straumann has written an article about this before. A working solution, but not too nice though.

Here's another solution which is unsupported (ofcourse ;) ), but it gives you a lot more freedom.

So how to tackle this problem. We already know how to hide and show fields. Now wouldn't it be great if we know what user is logged in? We would then be able to say (ofcourse modify the loggedInUser id with the guid of the user you need):

if (loggedInUser == '{123456789-1234-5678-9012-123456789012}'{
crmForm.all.name_c.style.display = 'none';
crmForm.all.name_d.style.display = 'none';
} else{
crmForm.all.name_c.style.display = 'block';
crmForm.all.name_d.style.display = 'block';
}

Now... how do we get that loggedInUser?
This is where it gets quite tricky. You can open the aspx page where you want to use this code. So why not add a little bit of asp coding in there? Lets say you want to use the code on the contact page. Then go to the server and open the "/SFA/conts/edit.aspx" page. Add this piece of code just before another script starts:

<script language="javascript">
var loggedInUser = '<%=Microsoft.Crm.Security.User.Current.UserAuth.UserId%>';
</script>


And now we have the loggedInUser! Save the file, store the javascript in the page onload and publish the contact. You will now have a page special for one user.
You can extend the codes as much as you like. You con for instance change the
Microsoft.Crm.Security.User.Current.UserAuth.UserId
with
Microsoft.Crm.Security.User.Current.IsSysAdmin
.
This all is undocumented and unsupported. Therefore you should only try these kind of modifications if you feel comfortable with this.

Good luck!

17 comments:

  1. Thank you Ronald,

    this is a really cool hack. I really like that.

    Cheers

    Arne

    ReplyDelete
  2. COOOOL=) THX.

    but this information is still on form and the user can reached it.
    I fink it`s better to use HTTP handler which remove unnessasary information from the form.

    ReplyDelete
  3. Hi Gaploid,

    An HTTP handler seems nicer indeed. This example is only to show what's possible.

    Just dont forget that the HTTP handler runs on every file that is loaded from the server!

    ReplyDelete
  4. I don't think your solution is too nice either bud.

    LOL.

    John.

    ReplyDelete
  5. John,

    Thanks for sharing your opinion. Could you provide us the solution you prefer, if possible including a sample?

    Ronald

    ReplyDelete
  6. Hi Ronald,

    Your trick is very cool...

    Thanks for sharing.

    Greg

    ReplyDelete
  7. Hi Ronald,
    I wish to apply your solution on a little problem but without success. Maybe you can help me :
    I have a picklist with 4 possibilities (Enlevé, Expédié, Livraison, Pose) (in french ;)
    Is it possible to show new different field according to the choice of the user in the picklist.
    Thanks for your help.

    Loïc

    ReplyDelete
  8. The names in the picklist are the Am I correct that you are searching for an onchange event for your picklist?

    Then you can use a script like this:

    if (crmForm.all.(picklistitem).DataValue == 1){
    crmForm.all.name_c.style.display = 'none';
    crmForm.all.name_d.style.display = 'none';
    crmForm.all.field2_c.style.display = 'block';
    crmForm.all.field2_d.style.display = 'block';
    }else if (crmForm.all.(picklistitem).DataValue == 2){
    crmForm.all.name_c.style.display = 'block';
    crmForm.all.name_d.style.display = 'block';
    crmForm.all.field2_c.style.display = 'none';
    crmForm.all.field2_d.style.display = 'none';
    }

    Hope this helps,

    Ronald

    ReplyDelete
  9. Great !!!!!!
    It works very well !!
    You just have to remember to create a new OnLoad event with the "null" result + the other in order to keep your record when you save your form.

    Thanks a lot for your help !

    Loïc

    ReplyDelete
  10. Hi,
    Do you think same solution can be used to extract the logon id rather than guid? If yes, can you point out which class holds the object/field? Will really appriciate it.
    Thanks.

    Regards,

    Sanjay

    ReplyDelete
  11. Hi Sanjay,

    You can't get the loginname like that. What you can do is:
    - write a webservice which accepts a username and returns a guid
    - create some javascript code which calls the webservice you just created (see crm sdk for example of webservice calling by javascript)
    - modify the code in my example to contain something like:
    if (loggedInUser == YourWebServiceCall('domain\user')){
    ...

    Hope this helps,

    Ronald

    ReplyDelete
  12. Ronald

    Nice one!

    This is a good example for field level security based on current user.

    Is it also possible to perform current user ROLE based security? How can I expose current user's role to Javascript instead of userID and use it to hide a field or disable changes to a field ?

    Thanks

    Aldrik

    ReplyDelete
  13. Hi Alderik,

    Look here:
    http://ronaldlemmen.blogspot.com/2006/05/finally-there-show-and-hide-fields.html

    Kind regards,
    Ronald

    ReplyDelete
  14. Only FYI, in outllok client/laptop it doesn't work.

    perhaps tha variable

    Microsoft.Crm.Security.User.Current.UserAuth.UserId

    is not accessible from outlook?

    Best regards,

    ReplyDelete
  15. Ronald,

    And hide and show fields based on teams???

    Hope this helps,

    Thank you.

    ReplyDelete
  16. Anonymous, look here:
    http://ronaldlemmen.blogspot.com/2006/05/finally-there-show-and-hide-fields.html
    That should get you started. Also note in the 'updates' at the end of the post links to CRM 4.0 codes.

    ReplyDelete
  17. Hi All, would you be able to help me to find out current user's Team which will help me to hide/unhide certain fields in CRM form?

    your early concern would be highly appreciated.

    thanks in advance

    ReplyDelete

Note: Only a member of this blog may post a comment.