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

If any case created, then check for the same user how many cases are created with in 30 days, if more than 2 and less than 5 send a mail to team lead, if more than 5 and less than 9 then send a mail to manager using power automate.

Power Apps Cheat Sheet