Using the IOrganizationService and IDiscoveryService

The XrmLibrary supports calls to the CRM organization and discovery services to retrieve data, metadata, and organization information. To wire up the application to these various services you need to configure the XrmLibrary module of the Config File.

Configuring the Services

  • It is recommended that you use the sample Config File as a guide to ensure you have both the log4net and XrmLibrary required config sections.
  • Once the configSections node has been setup, to connect to a single CRM organization use the following code:
<XrmLibrary>
  <CrmServiceConfigurations>
    <add CrmOrganizationUrl="https://<CrmServerUrl>/XRMServices/2011/Organization.svc" CrmDiscoveryUrl="https://<DiscoveryServerUrl>/XRMServices/2011/Discovery.svc" CrmAuthenticationType="<AD/IFD/Passport>" CrmUserName="********" CrmPassword="********" />
  </CrmServiceConfigurations>
</XrmLibrary
  • CrmOrganizationUrl and CrmDiscoveryUrl can be found in the developer section of the customizations area of Microsoft Dynamics CRM.
  • The CrmDiscoveryUrl is optional and will only be used if requests to the discovery service are made. Within code, use the Services.Instance.IsCrmDiscoveryServiceConfigured property to tell if the CRM discovery service has been configured. If multiple CRM organizations have been configured, use the Services.Instance.GetIsCrmDiscoveryServiceConfigured method passing the ordinal of the organization configuration.
  • Set the CrmAuthenticationType to the following values depending on the current CRM version:
    • AD: The environment is CRM On-Premise.
    • IFD: The environment is setup as Internet Facing Deployment.
    • Passport: You are connecting to Microsoft CRM 2011 Online.
  • CrmUserName and CrmPassword are required.

Using the Services in Code

  • To access the services within your code, use the following code:
// Get an instances of the default organization service.
using(var orgService = Services.Instance.OrganizationService)
{
    // orgService is an instance of IOrganizationService.
}

// Check to see if it exists and get an instance of the default CRM discovery service.
if(Services.Instance.IsCrmDiscoveryServiceConfigured)
{
    using(var discoService = Services.Instance.DiscoveryService)
    {
        // discoService is an instance of IDiscoveryService.
    }
}

  • To get instances other than the default (first ordinal in the config file organization list), use the Services.Instance.GetOrganizationService method and pass the ordinal of the organization position that you would like to retrieve to get the corresponding IOrganizationService instance. Similarly, use the Services.Instance.GetDiscoveryService method and pass the ordinal of the organization position to retrieve the corresponding IDiscoveryService instance.
  • The ordinal parameters are 0 based, so to get the default ordinal, use the following code:
using(var service = Services.Instance.GetOrganizationService(0))
{
}
  • These instances can be mixed and matched for all organizations configured in the Config File as shown below:
// Instantiate three IOrganizationService instances and use them for integration with multiple environments.
using(var firstService = Services.Instance.GetOrganizationService(0))
{
    using(var secondService = Services.Instance.GetOrganizationService(1))
    {
        using(var thirdService = Services.Instance.GetOrganizationService(2))
        {
            // Use all three organization services as needed.
        }
    }
}

Last edited Nov 1, 2011 at 1:05 AM by cchamberlain, version 2

Comments

No comments yet.