public static IOrganizationService GetCRMConnection(bool CheckConnection = false)
{
string crmServerName = ConfigurationManager.AppSettings["crmServerName"].ToString(); //--- You can also put IP address of your CRM server
string crmOrganizationName = ConfigurationManager.AppSettings["crmOrganizationName"].ToString();
string userName = ConfigurationManager.AppSettings["crmuserName"].ToString();
string password = ConfigurationManager.AppSettings["crmpassword"].ToString();
string protocol = ConfigurationManager.AppSettings["crmprotocol"].ToString();
ClientCredentials credentials = new ClientCredentials();
credentials.UserName.UserName = userName;
credentials.UserName.Password = password;
Uri serviceUri = new Uri(protocol + "://" + crmServerName + "/" + crmOrganizationName + "/XRMServices/2011/Organization.svc");
OrganizationServiceProxy proxy = new OrganizationServiceProxy(serviceUri, null, credentials, null);
proxy.EnableProxyTypes();
IOrganizationService crmService = (IOrganizationService)proxy;
if (CheckConnection)
{
Guid userid = ((WhoAmIResponse)crmService.Execute(new WhoAmIRequest())).UserId;
}
return crmService;
}