Connect to your instance via C# for validation

Hi Folks,

Every now and then when we were struck up with the issues with our code or with any power platform components like Power Automate, we had to definitely understand whether it is working from normal C# console application.

There were tons of articles over the internet and just it’s ophhhh…

So this post is just for a quick reference to connect to Dynamics 365 CE Instance to check if your actual function is returning valid data or not. I have just removed my functions for brevity.

You can just specify your Client Id, Client Secret, Instance URL below.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Crm.Sdk;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Tooling.Connector;
using System.ServiceModel;

namespace TestConsole
{
internal class Program
{
static void Main(string[] args)
{

        IOrganizationService orgService = GetOrganizationServiceClientSecret(your client id, your client secret, instance url);
        var response = orgService.Execute(new WhoAmIRequest());//To validate the request

        if(response != null)
        {
            Console.WriteLine("Connected to CRM");

            //Your function here
        }
    }

    public static IOrganizationService GetOrganizationServiceClientSecret(string clientId, string clientSecret, string organizationUri)
    {
        try
        {
            var conn = new CrmServiceClient($@"AuthType=ClientSecret;url={organizationUri};ClientId={clientId};ClientSecret={clientSecret}");
            return conn.OrganizationWebProxyClient != null ? conn.OrganizationWebProxyClient : (IOrganizationService)conn.OrganizationServiceProxy;
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error while connecting to CRM " + ex.Message);
            Console.ReadKey();
            return null;
        }
    }
}

}

Whenever you want to check any thing from C# Console, I hope this piece code works for connecting to your Dynamics.

Cheers,

PMDY

Advertisement
SharePains by Microsoft MVP Pieter Veenstra

Microsoft 365, Power Platform, SharePoint, Teams, Azure and Dynamics

D365 Demystified

A closer look at Microsoft Dynamics 365.

ECELLORS CRM Blog

Sharing thoughts and challenges on Power Platform & Azure along with respective solutions put together...

Vicky Rodgers - Microsoft Dynamics 365

Everything Microsoft Dynamics 365 for Customer Engagement

XRM Tricks (Power Platform & Dynamics CRM )

Power Platform & Dynamics CRM

Microsoft Dynamics 365 UK MCT

All about Microsoft Dynamics 365

High Voltages

Arduino | ESP | Raspberry pi | IoT

Mihail's space

Just another WordPress.com site

Arun Potti's MS CRM blog

Microsoft Dynamics CRM

Sander van de Velde

Microsoft MVP Azure | IoT Platform Architect | Speaker about IoT | Let me add some value

Power Platform Blog

Author : Dharanidharan Balasubramaniam

That API Guy

Making the world a better place one PowerApp/Flow at a time!

Dynamics Back Page

Tips, Tricks, and General Musings on Dynamics 365 (CRM), the Power Platform, Power Apps, Power Automate, Power BI and related technologies

Azure Integration services for Dynamics 365

Azure Integration services for Dynamics 365 Unified Operations, Logic Apps, Azure Functions, REST

Power Maverick

Incredible journey into #Dynamics365, #CDS, #PowerApps #Azure and all good stuff that #Microsoft releases

Unleashing CRM

My Technical Thoughts about Microsoft Dynamics CRM- Unleashed

Srikanth Alluri

My experiences in & around Power Platform + Azure

Debajit's Power Apps & Dynamics 365 Blog

All about Power Apps & Dynamics

Rajeev Pentyala - Microsoft Power Platform

Sharing my knowledge on Power Platform, Dynamics 365, Azure & .Net Stack

Bansal Blogs - Dynamics 365, PowerApps, Microsoft Flows, Power BI

Programming demands a lot more than simple mastery in tools and technologies

%d bloggers like this: