How to get the logged in user using C# and UserId

 

using Microsoft.Xrm.Tooling.Connector;

using Microsoft.Xrm.Sdk;

using Microsoft.Crm.Sdk.Messages;

using Microsoft.Xrm.Sdk.Query;

Add the code. We will first get the user id, then perform a retrieve to get the user’s name:

try

{


    var connectionString = @"AuthType = Office365; 

    Url = https://yourtenant.crm.dynamics.com/;

    Username=yourusername;

    Password=yourpassword";

    CrmServiceClient conn = new CrmServiceClient(connectionString);

    IOrganizationService service = (IOrganizationService)conn.OrganizationWebProxyClient != null ?                       (IOrganizationService)conn.OrganizationWebProxyClient ;

   (IOrganizationService)conn.OrganizationServiceProxy;

   

 // Get a system user to send the email (From: field)

    WhoAmIRequest systemUserRequest = new WhoAmIRequest();

    WhoAmIResponse systemUserResponse = (WhoAmIResponse)service.Execute(systemUserRequest);

    Guid userId = systemUserResponse.UserId;


    // Lookup User

    var User = service.Retrieve("systemuser", userId, new ColumnSet("fullname"));

    string fullName = User["fullname"].ToString();

    Console.WriteLine(userId.ToString());

    Console.WriteLine(fullName);

    Console.ReadLine();

}

catch (Exception ex)

{

    // Implement error handling

}

Comments

Popular posts from this blog

Relationship Behaviours in D365 crm

Call Action Through JavaScript

Power Apps Cheat Sheet