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:
<httpModules> <add name ="MapOrg" type="Microsoft.Crm.MapOrgEngine, Microsoft.Crm, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/> <add name ="CrmAuthentication" type="Microsoft.Crm.Authentication.AuthenticationEngine, Microsoft.Crm, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/> </httpModules>
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() .CallerId; } else { string orgname = Request["orgname"]; return Microsoft.Crm.Sdk.CrmAuthenticationToken .ExtractCrmAuthenticationToken(Context, orgname) .CallerId; } }
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="window.open( '../../../ISV/path/to/mypage.aspx?orgname=' + ORG_UNIQUE_NAME + '&other-params');">
(thanks to SO for this hint)