How to connect to Dynamics CRM Onpremises AD/IFD from a Windows Form Application

Being a CRM developer, you often need to develop some external application. In this blog i will talk about and provide the code to connect to dynamics CRM Onpremises in a windows form application.

Login

public static ConnectToCrm(string username, string password, string domain, string Url)
{

ClientCredentials _clientCreds = new ClientCredentials();
_clientCreds.Windows.ClientCredential.UserName = username;
_clientCreds.Windows.ClientCredential.Password = password;
_clientCreds.Windows.ClientCredential.Domain = domain;

if (username == "" || password == "" || domain == "" || Url == "")
{
MessageBox.Show("Please Enter All Details To Login!");
}

_service = (IOrganizationService)new OrganizationServiceProxy(new Uri(Url), null, _clientCreds, null);

OrganizationServiceContext _orgContext = new OrganizationServiceContext(_service);
Guid orgId = ((WhoAmIResponse)_service.Execute(new WhoAmIRequest())).OrganizationId;

if (orgId != null)
{
MessageBox.Show("Successfully Connected to CRM. Click OK");
}
else if(orgId==null)
{
MessageBox.Show("Could Not Connect to CRM. Click OK & Try Again!");
}
}

The _service can be utilized in your methods to retrieve the data and perform actions in dynamics CRM. i.e, below:

var fetchExpression = new FetchExpression(fetchXml);
EntityCollection fetchResult = _service.RetrieveMultiple(fetchExpression);

hope this helps!

cheers!

Advertisement

RIP SDK – Dynamics CRM 365 v9

As per the recent communication from Microsoft, Microsoft Dynamics CRM SDK will not be available on Microsoft downloads. If you are working on Dynamics 365 v9, well you have to download every component which was available in the SDK individually.

Every tool will be available via NuGet Manager.

The name will be : Developer Guide not Dynamics CRM SDK.

For more information, visit the below blog :  https://blogs.msdn.microsoft.com/crm/2017/11/01/whats-new-for-customer-engagement-developer-documentation-in-version-9-0

Here is the step by step information on how to download these tools using powershell :  https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/download-tools-nuget

 

i hope this helps.

Cheers!