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!