Retrieving the Current User in Dynamics CRM 4 ISV ASP.Net

If you are manipulating user-related data in a Dynamics CRM ASP.Net application (i.e. under the ISV sub-directory), you need to retrieve the currently logged-on user, for example to set the ownerid value for a newly created record belonging to the current user..

Initial research led me to the WhoAmIRequest class which I excepted to return the current systemuser (as many others).

However, what the request really returns is the systemuser associated with the Dynamics login credentials, as the vaguely formulated introduction on MSDN suggests:

Contains the data needed toretrieve the system user ID for the currently logged on user or the user under whose context the code is running

To retrieve the current user, the CRM-related HttpModules are required in the web.config:

  <add name ="MapOrg" 
    type="Microsoft.Crm.MapOrgEngine, Microsoft.Crm,
      Version=, Culture=neutral, 
  <add name ="CrmAuthentication" 
      Microsoft.Crm, Version=, Culture=neutral, 

These modules are typically included in the CRM’s web.config, so there should be no need to include them in ISV as well. You must not remove them using the <remove name=”CrmAuthentication” /> tag!

You can then go ahead and create a CrmAuthenticationToken from the web request using the static method CrmAuthenticationToken.ExtractCrmAuthenticationToken():

Guid GetCallingUserId()
  if (Request.IsLocal)
    return new Microsoft.Crm.Sdk.CrmAuthenticationToken()
    string orgname = Request["orgname"];
    return Microsoft.Crm.Sdk.CrmAuthenticationToken
      .ExtractCrmAuthenticationToken(Context, orgname)

How do you pass the organization name to the .aspx?

In the ISV.config, set the MenuItem‘s PassParams attribute to “1” if the menu item defines a Url property.

If the menu item defines a JavaScript attribute to execute JavaScript upon selection, include the ORG_UNIQUE_NAME variable in the JS code:

<MenuItem JavaScript="
  + ORG_UNIQUE_NAME + '&amp;other-params');">

(thanks to SO for this hint)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: