POWEROBJECTS

718 Washington Ave. N. Suite #101
Minneapolis, MN 55401
View map and all Locations

Send us a message

Ever came across a scenario when you need to get the currently logged in CRM user within your custom page? Normally one would use WhoAmIRequest. It works as long as you are running an on-premise version. For IFD scenarios this throws exception. On some blogs people have suggested to use CrmImpersonator. Using CrmImpersonator doesn’t throw an exception in either scenarios (on-prem and IFD), but it returns the SYSTEM user.

crm2011

Here is how you can obtain the correct CRM user for these 2 scenarios:

//Check if user is connected for on-prem or hosted

Guid userGuid;

if(Request.LogonUserIdentity.IsAuthenticated == true)

{

    WhoAmIRequest userRequest = new Microsoft.Crm.SdkTypeProxy.WhoAmIRequest();

    WhoAmIResponse user = (Microsoft.Crm.SdkTypeProxy.WhoAmIResponse)objCrmService.Execute(userRequest);

    userGuid = user.UserId;

}

else //ifd

{

     userGuid = new Guid(Context.User.Identity.Name);

}

//Populating a dropdown with all users and selecting the current user

SelectUser(userGuid);

private void SelectUser(Guid userId)

{   

BusinessEntityCollection users = LoadUsers();//Load all users

       for (int i = 0; i < users.BusinessEntities.Count; i++)

       {

           DynamicEntity entity = (DynamicEntity)users.BusinessEntities[i];

           if (entity.Properties.Contains(“systemuserid”) && ((Key)entity.Properties[“systemuserid”]).Value == userId)

           {

               DDLUsers.Items.Add(new ListItem(entity.Properties[“fullname”].ToString(), ((Key)entity.Properties[“systemuserid”]).Value.ToString()));

               DDLUsers.Items[i].Selected = true;

           }

           else

           {

               DDLUsers.Items.Add(new ListItem(entity.Properties[“fullname”].ToString(), ((Key)entity.Properties[“systemuserid”]).Value.ToString()));

            

           }

       }

}

 Happy Programming!

Avatar for JoeCRM

JoeCRM

Joe CRM is a CRM superhero who runs on pure Microsoft Dynamics CRM adrenaline. As the face of PowerObjects, Joe CRM’s mission is to reveal innovative ways to use Dynamics CRM and bring the application to more businesses and organizations around the world.