Advertisements
Home > Dynamics 365 > Code snippet – Connect to Dynamics CRM using Organization Service C#

Code snippet – Connect to Dynamics CRM using Organization Service C#

Below is the sample code to connect to Dynamics 365/CRM online 2016 using simplified connection.

This sample authenticates the user with the Dynamics 365 web services by using the CrmServiceClient and methods.

Required CRM SDK .Dlls:

  • Download the SDK to get the required .dlls
Required dlls

Required dlls

Required Namespaces :

using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Tooling.Connector;

Configure connection string in App.Config:

O365 Connection strings

O365 Connection strings

C# Code:

try{
String connectionString = GetServiceConfiguration();
CrmServiceClient conn = new CrmServiceClient(connectionString);

// Uncomment below statement in case you want to use Organization Service
// IOrganizationService orgService = conn.OrganizationWebProxyClient != null ? conn.OrganizationWebProxyClient : (IOrganizationService)conn.OrganizationServiceProxy;

OrganizationServiceProxy orgService = conn.OrganizationServiceProxy;

// To impersonate, set the caller GUID
orgService.CallerId = new Guid(“D0DED6CA-5A65-E611-80DD-FC15B42877A8”);

// Create Account

Entity account = new Entity(“account”);
account[“name”] = “Peter Parker”;
Guid accountId = orgService.Create(account);

// Who Am I Request

// Guid userid = ((WhoAmIResponse)orgService.Execute(new WhoAmIRequest())).UserId;

Console.ReadLine();
}
catch (Exception ex){
Console.WriteLine(“Exception : ” + ex.Message);
Console.ReadLine();
}

// To Read Connection Strings From <App.Config>

private String GetServiceConfiguration(){
// Get available connection strings from app.config.
int count = ConfigurationManager.ConnectionStrings.Count;

// Create a filter list of connection strings so that we have a list of valid
// connection strings for Microsoft Dynamics CRM only.
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 one valid connection string is found, use that.
if (filteredConnectionStrings.Count == 1)
{
return filteredConnectionStrings[0].Value;
}

return null;
}

private Boolean isValidConnectionString(String connectionString){
// At a minimum, a connection string must contain one of these arguments.
if (connectionString.Contains(“Url=”) ||
connectionString.Contains(“Server=”) ||
connectionString.Contains(“ServiceUri=”))
return true;

return false;
}

Refer MSDN article for more details.

🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: