If you need to quickly connect to the Dataverse WebAPI from a C# console and perform testing, this article will provide a code snippet for establishing a connection to the Dataverse WebAPI. We’ll explore the following connectivity options:
- Connecting via Client ID and Secret
- Connecting via User Id and Password
Connect Dataverse Web API using using Client Id and Secret:
As a prerequisite, make sure you have followed these steps and registered an app with Azure Active Directory.
Code Snippet:
static string url = "https://xxxxxxx.crm.dynamics.com";
static string clientId = "xxxx-7d52-454d-9499-xxxxxx";
static string clientSecret = "xxxxxxx-Tsj4Nde3SONsomxxxxxxXG";
// This service connection string uses the info provided above.
// The AppId and RedirectUri are provided for sample code testing.
static string connectionString = $@"
AuthType = ClientSecret;
Url = {url};
ClientId = {clientId};
ClientSecret = {clientSecret};";
static void Main(string[] args)
{
using (ServiceClient serviceClient = new ServiceClient(connectionString))
{
if (serviceClient.IsReady)
{
WhoAmIResponse response = (WhoAmIResponse)serviceClient.Execute(new WhoAmIRequest());
Console.WriteLine("User ID is {0}.", response.UserId);
}
}
}

Connect Dataverse Web API using User Id and Password:
Code snippet:
static string url = "https://xxxxx.crm.dynamics.com/";
static string userName = "xxxx@xxxxxxx.onmicrosoft.com";
static string password = "xxxxxxxx";
// This service connection string uses the info provided above.
// The AppId and RedirectUri are provided for sample code testing.
static string connectionString = $@"
AuthType = OAuth;
Url = {url};
UserName = {userName};
Password = {password};
AppId = 51f81489-12ee-4a9e-aaae-a2591f45987d;
RedirectUri = app://58145B91-0C36-4500-8554-080854F2AC97;
LoginPrompt=Auto;
RequireNewInstance = True";
static void Main(string[] args)
{
using (ServiceClient serviceClient = new ServiceClient(connectionString))
{
if (serviceClient.IsReady)
{
WhoAmIResponse response = (WhoAmIResponse)serviceClient.Execute(new WhoAmIRequest());
Console.WriteLine("User ID is {0}.", response.UserId);
}
}
}

🙂


![[Beginners] Power Fx: ShowColumns, AddColumns, RenameColumns and DropColumns](https://rajeevpentyala.com/wp-content/uploads/2024/04/record-ezgif.com-video-to-gif-converter-1-2.gif)
Leave a comment