WebMasterSam

.Net, SEO, Dynamics CRM, AdSense/AdWords, Dating sites, Silverlight, Web hosting and more

About me

I'm an IT consultant working primarily with the .Net Framework as a developper and architect. I also work on my own on my personnal dating websites. I've been developping websites since 2000.

If you like what I do, feel free to support me

PayPal - The safer, easier way to pay online!

Bookmark

Bookmark and Share

Last comments

None

How to know if a user is in a specified role in Microsoft Dynamics CRM 4.0 (JavaScript)

In CRM 4.0, when you want to know if the currently logged in user is in a certain security role with JavaScript, you need
to issue a request to the server. Yes, this is the officially supported way to do this, but i'll explain to you a good solution
to avoid doing a lot of code to call the server with AJAX.

The solution I've developped is a lot more performant because you don't need to call (AJAXly) the server, but it is officially
unsupported because you have to inect some code in the CRM pages at runtime.

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).

In your HttpModule you simply have to call the CrmService web reference to get all the roles of the currently logged in user.
After you get this, you build a string that correspond to a JavaScript Array of "typed" objects.

Yeah, in JavaScript everything is untyped; you all declare variables with the "var" keyword. But, you can build custom objects
and set your own properties on it.

Lets say you do this :

var objRole = new Object();

objRole.GUID = [GUID of the CRM security role];
objRole.Name = [Name of the CRM security role];

You get an object representing your security role with all the informations you need. 

So, you have to build (with a StringBuilder) the JavaScript string that will outputed to the CRM page at runtime.

Let's do something like this in code :

StringBuilder strJS = new StringBuilder();

strJS.AppendLine("var SECURITY_ROLES = new Array();");

foreach (CrmService.role objRole in securityRoles)
{
    strJS.AppendLine("SECURITY_ROLES[SECURITY_ROLES.length] = new Object();");

    strJS.AppendLine(String.Format("SECURITY_ROLES[SECURITY_ROLES.length].Name = '{0}';", objRole.name));
    strJS.AppendLine(String.Format("SECURITY_ROLES[SECURITY_ROLES.length].GUID = '{0}';", objRole.roleid));
}

string javaScriptString = strJS.ToString();

Then, you just inject thoses lines of JS code : [HeadTag].Append(javaScriptString);

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 array called SECURITY_ROLES containing all the roles of the current user.
So if you want to check if the user is part of a group, you can check that array instead of issuing a lot of AJAX request to the server at runtime.
Posted: Apr 10 2009, 06:28 by WebMasterSam | Comments (0) RSS comment feed |
  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Filed under: .Net | CRM 4.0 | JavaScript
Social Bookmarks: E-mail | Kick it! | DZone it! | del.icio.us
LINK BUILDING IS PROHIBITED ON THIS WEBSITE