How to get the username client-side in Microsoft Dynamics CRM 4.0
In CRM 4.0, when you want to know to NT username of the currently logged in user with JavaScript, you need
to issue a request to the server. This sounds (and IS) ridiculous and also no performant.
The solution I've developped is a lot more performant because you don't need to call (AJAXly) the server
to get the username of the current user.
HttpModules...
For a lot of things in CRM we've used HttpModules to inject some JavaScript code onto the CRM pages (in the <head> tag).
For the currently logged in user we just inject this line of .Net code : [HeadTag].AppendFormat("var USERNAME='{0}';", My.User.Name);
The [HeadTag] is a class that we developped to encapsulate the stream property named "Filter" of the HttpResponse object, accessible
on the HttpModule. This property contains the HTML to is about to be sent to the client so we use this to inject our JavaScript code.
With this solution, in each page in CRM you have a JavaScript variable called USERNAME that you can use to have the username !
You can also do the same thing to have the current computer name.