How to connect to CRM using SDK and NuGet
Once you have installed the SDK on your machine and have an environment to use, then now it is time to connect to CRM using C# so you can use workflows, plugins etc.
I have already posted a blog here on How to connect to CRM using SDK without NuGet. Here we will discuss on how to do it more efficiently.
Install the NuGet
Open the Nuget packages for solution in visual studio and browse these packages:
- Microsoft.CrmSdk.CoreAssemblies
- Microsoft.CrmSdk.XrmTooling.CoreAssembly
When you install both of them, there are other packages which will also be installed by default.
Since you have not added the namespaces into your class file, just right click on any class which is showing error and select ‘Quick action and refactoring‘ option and select the namespaces to be added to your class file.
Connect & Query your data
Before you connect to CRM you need to create the connection string to CRM. I have mentioned few samples below:
<!– Online using Office 365 –>
<!–<add name=”Server=CRM Online”
connectionString=”Url=https://myserver.crm.dynamics.com; Username=user@myserver.com; Password=password; authtype=Office365″/>–>
<!– On-premises with provided user credentials –>
<!– <add name=”Server=myserver, organization=AdventureWorksCycle, user=administrator”
connectionString=”Url=http://myserver/AdventureWorksCycle; Domain=mydomain; Username=administrator; Password=password; authtype=AD”/> –>
<!– On-premises using Windows integrated security –>
<!– <add name=”Server=myserver, organization=AdventureWorksCycle”
connectionString=”Url=http://myserver/AdventureWorksCycle; authtype=AD”/> –>
<!– On-Premises (IFD) with claims –>
<!–<add name=”Server=myserver.com, organization=contoso, user=someone@myserver.com”
connectionString=”Url=https://contoso.myserver.com/contoso; Username=someone@myserver.com; Password=password; authtype=IFD”/>–>
- Now it is time to instantiate the CrmServiceClient Class with the connection string.
CrmServiceClient conn = new CrmServiceClient(ConfigurationManager.ConnectionStrings[“Server=CRM Online”].ToString());
var _orgService = (IOrganizationService)conn.OrganizationServiceProxy;
Now using the _orgService object you can create queries to fetch data or can also use to Insert/Update/Delete data into CRM.

This was originally posted here.
*This post is locked for comments