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

No comments

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!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.