Monday, August 5, 2019

How to connect Dynamic CRM online using C#


Here are the steps to get connected to dynamic CRM using Tooling connector and do some sample retrieve and delete.

Step 1) Gather around Dynamic CRM DLL’s
We have two options either get DLL’s manually or make use of NuGet packages.



Here is a list of DLL’s which are used to get connected to Dynamic CRM Online instance.



Note: Please note that DLL’s are Framework dependent and try setting Framework to either 4.5.2 (minimum for v8) or 4.6.2 (minimum for v9 onwards)


Step 2) Code to Connect to Dynamic CRM Online Instance.

public static IOrganizationService getDevOrgService()
{
    IOrganizationService OrgService = null;
    ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
    CrmServiceClient crmSvc = new CrmServiceClient("vipin.jaiswal@vjCity.onmicrosoft.com",
        CrmServiceClient.MakeSecureString("Your_Password"),
        "EMEA",
        "crmOrgName",
        true,
        isOffice365: true);

    if(crmSvc.IsReady)
        OrgService = (IOrganizationService)crmSvc.OrganizationWebProxyClient != null ?
            (IOrganizationService)crmSvc.OrganizationWebProxyClient :
                (IOrganizationService)crmSvc.OrganizationServiceProxy;

    return OrgService;

}

CrmServiceClient Syntax

CrmServiceClient("UserName","Password","CrmRegion","OrgName",useUniqueInstance:false, useSsl:false,orgDetail,isOffice365:true);

Some values for the CrmRegion parameter are: 

https://docs.microsoft.com/en-us/power-platform/admin/new-datacenter-regions

  • NAM      crm.dynamics.com
  • DEU       crm.microsoftdynamics.de
  • SAM       crm2.dynamics.com
  • CAN       crm3.dynamics.com
  • EUR       crm4.dynamics.com
  • FRA        crm12.dynamics.com
  • APJ         crm5.dynamics.com
  • OCE       crm6.dynamics.com
  • JPN        crm7.dynamics.com
  • IND        crm8.dynamics.com
  • GCC       crm9.dynamics.com
  • GBR       crm11.dynamics.com
  • ZAF         crm14.dynamics.com
  • UAE       crm15.dynamics.com
  • GER        crm16.dynamics.com
  • CHE        crm17.dynamics.com
  • CHN       crm.dynamics.cn


If you set CrmRegion to String.Empty, it will search servers in all the regions for the Dynamics 365 organization. 

For the OrgName parameter, you can specify either the unique or friendly name.

The following parameters are optional: 

  • useUniqueInstance, 
  • useSsl, 
  • orgDetail


We can also connect using ConnectionString

  <connectionStrings>
    <add name="CrmServerConnection" connectionString="AuthType=Office365;Username=vipin.jaiswal@ekstart.com; Password=password;Url=https://ekstart-test.crm.dynamics.com; CrmRegion=NorthAmerica"/>
  </connectionStrings>

string connectionString = ConfigurationManager.ConnectionStrings["CrmServerConnection"].ConnectionString;
CrmServiceClient crmSvc = new CrmServiceClient(connectionString);
if (crmSvc.IsReady == true)
{
    IOrganizationService _orgService88 = (IOrganizationService)crmSvc.OrganizationWebProxyClient != null ?
        (IOrganizationService)crmSvc.OrganizationWebProxyClient : (IOrganizationService)crmSvc.OrganizationServiceProxy;
}


RequireNewInstance=True
You can also add this parameter in ConnectionString, if you are making multiple connection to Dynamic CRM within single console application.


Step 3) Make CRUD Operation

Once you are able to obtain the IOrganizationService which is in our code is neoOrgService, we can start using it for making a CRM operation like create, insert, update, delete, retrieve etc.

Here is a small code to fetch certain records and delete them if exists.




Regards,
Vipin Jaiswal
Vipinjaiswal12@gmail.com



No comments: