There are many ways to connect to and access the database of Dynamics CRM database.

a- If you are creating plug-ins, custom workflow activities, or custom XAML workflows, use Dynamics 365 SDK assemblies

https://msdn.microsoft.com/en-us/library/jj602917.aspx?f=255&MSPPError=-2147217396#SDKAssemblies

b- If you are creating Windows applications for Dynamics 365, use XRM tooling assemblies. More information: .NET Development: Use XRM Tooling assemblies

https://msdn.microsoft.com/en-us/library/jj602917.aspx?f=255&MSPPError=-2147217396#XrmTooling

c- If you are creating non-Windows applications for Dynamics 365, use Web API. More information: Use the Microsoft Dynamics 365 Web API

https://msdn.microsoft.com/en-us/library/mt593051.aspx

The way we will see in this article is to connect through CRM SDK and get the connection Service Proxy object.

In web.config file, add the following entry that carrys the connection string for CRM.

<add key=”XrmConnection” value=”Url=https://crm.domainname.com; Username={0}; password={1}” />

Add reference to following Dll’s

CRM SDK Reference

/// <summary>
/// Method globally used to return a CRM Service Proxy Object, which will be used to make futher calls to CRM.
/// </summary>
/// <param name=”userName”></param>
/// <param name=”password”></param>
/// <returns></returns>
private OrganizationService GetCRMService(string userName, string password)
{
try
{
CrmConnection connection =
CrmConnection.Parse(string.Format(ConfigurationManager.AppSettings[“XrmConnection”], userName, password));
OrganizationService orgService = new OrganizationService(connection);
return orgService;
}
catch (Exception ex)
{
return null;
}
}

This method will return the OrganizationService Proxy object that we can use later to fetch the data from Dynamics CRM Entities.

But before doing this we need to Authenticate and Authorise the user. There are two cases, one in which our application does not require the user to see his personalised content, instead the application is a reporting app, or a generic application for all the users of the organisation. In that case, we can fix the username and password for the master user account in web.config file. Make sure that user have enough previledges requried for the application to run.

In other case, if each user has to login into the application and perform his own activities, in that case above method of connection string and accessing through the metho