Looking for PowerObjects? Don’t worry, you’re in the right place! We’ve been part of HCL for several years, and we’ve now taken the final step in our acquisition journey: moving our website to the HCL domain. Nothing else is changing – we are still fanatically focused on Microsoft Business Applications!

PowerObjects Blog 

for Microsoft Business Applications


Get current user in CRM

Post Author: Joe D365 |

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.

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!

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

3 comments on “Get current user in CRM”

  1. This didn't work for me actually. In my case, both the CrmImpersonator and the Context.User.Identity.Name point to the *system* account, not the users account.

  2. Hi,

    Actually i am new to crm development i am having experience in .net what i want is to know the selected users from the leads. I mean the user details of the selected users in that searched grid.

PowerObjects Recommends