Connect/Authenticate Console App to MS Dynamics CRM/CE
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
public static OrganizationServiceProxy GetOrgServiceProxy(appConfigSettings settings, bool setEndpointBehavior)
{
// Create the service management object
var orgServiceManagement = ServiceConfigurationFactory.CreateManagement<IOrganizationService>(
new Uri(string.Format("{0}/{1}/XRMServices/2011/Organization.svc", settings.CrmServerUrl, settings.OrgName)));
// Optionally set endpoint behavior
if (setEndpointBehavior)
{
var behavior = new ProxyTypesBehavior() as IEndpointBehavior;
behavior.ApplyClientBehavior(orgServiceManagement.CurrentServiceEndpoint, null);
}
// Create authentication credentials
var authCredentials = new AuthenticationCredentials();
switch (orgServiceManagement.AuthenticationType)
{
case AuthenticationProviderType.ActiveDirectory: // Active Directory
authCredentials.ClientCredentials.Windows.ClientCredential = new NetworkCredential(settings.Username, settings.Password);
break;
default:
authCredentials.ClientCredentials.UserName.UserName = settings.Username;
authCredentials.ClientCredentials.UserName.Password = settings.Password;
break;
}
// Authenticate and get the service proxy
var authenticatedCredentials = orgServiceManagement.Authenticate(authCredentials);
return new OrganizationServiceProxy(orgServiceManagement, authenticatedCredentials.SecurityTokenResponse);
}
//App Config File
<appSettings>
<add key="orgName" value="crmdev" />
<add key="crmServerUrl" value="https://crmdev.business.com" />
<add key="username" value="service-account" />
<add key="password" value="Password@12345" />
</appSettings>
Let's break down the code line by line with references to relevant Microsoft articles:
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
- Microsoft.Xrm.Sdk: This namespace contains classes and interfaces for interacting with Microsoft Dataverse (formerly Dynamics CRM). It includes types for entities, attributes, and service operations[1].
- Microsoft.Xrm.Sdk.Client: This namespace provides classes for connecting to web services, sending requests, and receiving responses. It includes types for authentication and service proxies[2].
Method Definition
public static OrganizationServiceProxy GetOrgServiceProxy(appConfigSettings settings, bool setEndpointBehavior)
- Static Method: This method can be called without creating an instance of the class.
- Parameters:
appConfigSettings settings
: An object containing CRM settings such as server URL, organization name, username, and password.bool setEndpointBehavior
: A flag indicating whether to set endpoint behavior.
Create the Service Management Object
var orgServiceManagement = ServiceConfigurationFactory.CreateManagement<IOrganizationService>(
new Uri(string.Format("{0}/{1}/XRMServices/2011/Organization.svc", settings.CrmServerUrl, settings.OrgName)));
- ServiceConfigurationFactory.CreateManagement: This method creates a service management object for the specified service type (
IOrganizationService
) using the provided URI[3]. - URI Construction: Constructs the URI for the organization service endpoint using the CRM server URL and organization name.
Optionally Set Endpoint Behavior
if (setEndpointBehavior)
{
var behavior = new ProxyTypesBehavior() as IEndpointBehavior;
behavior.ApplyClientBehavior(orgServiceManagement.CurrentServiceEndpoint, null);
}
- ProxyTypesBehavior: This class enables early-bound entity types on a service proxy[4].
- ApplyClientBehavior: Applies the behavior to the current service endpoint[4].
Create Authentication Credentials
var authCredentials = new AuthenticationCredentials();
switch (orgServiceManagement.AuthenticationType)
{
case AuthenticationProviderType.ActiveDirectory: // Active Directory
authCredentials.ClientCredentials.Windows.ClientCredential = new NetworkCredential(settings.Username, settings.Password);
break;
default:
authCredentials.ClientCredentials.UserName.UserName = settings.Username;
authCredentials.ClientCredentials.UserName.Password = settings.Password;
break;
}
- AuthenticationCredentials: Represents client-side user logon credentials[5].
- AuthenticationProviderType: Determines the type of authentication (e.g., Active Directory or other types)[5].
- NetworkCredential: Sets the credentials for Windows authentication[5].
Authenticate and Get the Service Proxy
var authenticatedCredentials = orgServiceManagement.Authenticate(authCredentials);
return new OrganizationServiceProxy(orgServiceManagement, authenticatedCredentials.SecurityTokenResponse);
- Authenticate: Authenticates the user with the provided credentials[5].
- OrganizationServiceProxy: Provides an authenticated WCF channel to the organization service[6].
App Config File
<appSettings>
<add key="orgName" value="crmdev" />
<add key="crmServerUrl" value="https://crmdev.business.com" />
<add key="username" value="service-account" />
<add key="password" value="Password@12345" />
</appSettings>
- App Settings: Contains key-value pairs for configuration settings such as organization name, CRM server URL, username, and password.
Summary
- Namespaces:
Microsoft.Xrm.Sdk
andMicrosoft.Xrm.Sdk.Client
provide types for CRM interactions[1][2]. - Service Management: Created using
ServiceConfigurationFactory.CreateManagement
[3]. - Endpoint Behavior: Optionally set using
ProxyTypesBehavior
[4]. - Authentication: Managed using
AuthenticationCredentials
and authenticated withorgServiceManagement.Authenticate
[5]. - Service Proxy: Returned as an
OrganizationServiceProxy
[6].
References