Hi Amrutha,
Please refer below code its working for me on both type of instance:
public static IOrganizationService service;
//DLL reference
//using Microsoft.Xrm.Sdk.Client;
//using System.ServiceModel.Description;
//using System.Net;
//using Microsoft.Xrm.Sdk.Query;
//using Microsoft.Xrm.Sdk.Messages;
//using Microsoft.Crm.Sdk.Messages;
//using Microsoft.Xrm.Client.Services;
//using Microsoft.Xrm.Client;
public void MethodName()
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
Microsoft.Xrm.Client.CrmConnection connection = CrmConnection.Parse(GetServiceConfiguration());
service = new OrganizationService(connection);
{
//Your code here
}
}
public static String GetServiceConfiguration()
{
int count = ConfigurationManager.ConnectionStrings.Count;
List<KeyValuePair<String, String>> filteredConnectionStrings =
new List<KeyValuePair<String, String>>();
for (int a = 0; a < count; a++)
{
if (isValidConnectionString(ConfigurationManager.ConnectionStrings[a].ConnectionString))
filteredConnectionStrings.Add
(new KeyValuePair<string, string>
(ConfigurationManager.ConnectionStrings[a].Name,
ConfigurationManager.ConnectionStrings[a].ConnectionString));
}
if (filteredConnectionStrings.Count == 0)
{
Console.WriteLine("An app.config file containing at least one valid Microsoft Dynamics CRM " +
"connection string configuration must exist in the run-time folder.");
Console.WriteLine("\nThere are several commented out example connection strings in " +
"the provided app.config file. Uncomment one of them and modify the string according " +
"to your Microsoft Dynamics CRM installation. Then re-run the sample.");
return null;
}
if (filteredConnectionStrings.Count == 1)
{
return filteredConnectionStrings[0].Value;
}
return null;
}
private static Boolean isValidConnectionString(String connectionString)
{
if (connectionString.Contains("Url=") || connectionString.Contains("Server=") || connectionString.Contains("ServiceUri="))
return true;
return false;
}
//<configuration>
// <connectionStrings>
// <add name= "Xrm" connectionString = "Url=crmdomainname.crm11.dynamics.com; Username=yourusername; Password=Userpassword; authtype=Office365" />
//</connectionStrings>
//</configuration>