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

Create approve & reject ribbon buttons, once click on approve it should change the status field to approve.If clicked on reject it should change to Reject. Based on status field change trigger work flow to send a email to stating request is approved/Rejected.

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.

How to get and set values in plugins?