Showing posts with label How to connect in Dataverse. Show all posts
Showing posts with label How to connect in Dataverse. Show all posts

Thursday, 10 August 2023

Connect dataverse in Console Application c#

  Install "Microsoft.PowerPlatform.Dataverse.Client"


Using Microsoft.PowerPlatform.Dataverse.Client;

 public static void DataverseConnector()

        {

            try

            {

               string conn = @"AuthType=OAuth;Username=user@org.onmicrosoft.com;Password=password;Url=https://orgapp.crm.dynamics.com;TokenCacheStorePath=c:\MyTokenCache2;LoginPrompt=Auto";


                using (ServiceClient serviceClient = new ServiceClient(conn))

                {

                    if (serviceClient.IsReady)

                    {

                        WhoAmIResponse response =(WhoAmIResponse)serviceClient.Execute(new WhoAmIRequest());

                        Console.WriteLine("User ID is {0}.", response.UserId);

                    }

                    else

                    {

                        Console.WriteLine("A web service connection was not established.");

                    }

                }


                // Pause the console so it does not close.

                Console.WriteLine("Press any key to exit.");

                Console.ReadLine();

            }

            catch (Exception ex)

            {

                Console.Write(ex.Message);

            }

            finally

            {

                Console.Write("test");

            }

        }