Enter your email address:

Delivered by FeedBurner

Archives

Categories

Get current user in CRM

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!

  • Share/Bookmark

2 comments to Get current user in CRM

  • Max Metral

    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.

  • Farooq

    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.

Leave a Reply

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>