30 May 2012

CRM Online Discovery Service client sample

Microsoft allows us to have a 30-days trial online CRM. I've signed up and decided to connect to CRM Online services from my application. At first I've tried to connect to discovery service. The CRM SDK has a sample sdk\samplecode\cs\generalprogramming\discoveryservice\discoveryservice.sln that shows us how to do that. This sample needs Credentials.xml file to be present in your <DISK>:\Users\<user>\AppData\Roaming\CrmServer folder.
Below is sample of this file.


<Configurations>
  <Configuration>
    <ServerAddress>your-org-name.crm.dynamics.com</ServerAddress>
    <OrganizationName> your-org-name </OrganizationName>
    <DiscoveryUri>https:// your-org-name .crm.dynamics.com/XRMServices/2011/Discovery.svc</DiscoveryUri>
    <OrganizationUri>https:// your-org-name .crm.dynamics.com/XRMServices/2011/Organization.svc</OrganizationUri>
    <HomeRealmUri></HomeRealmUri>
    <Credentials>
      <UserName>live ID</UserName>
      <Password>live ID password</Password>
    </Credentials>
    <EndpointType>LiveId</EndpointType>
    <UserPrincipalName></UserPrincipalName>
    <Xpert360Lightning>
      <LicenseKey></LicenseKey>
    </Xpert360Lightning>
  </Configuration>
</Configurations>

This sample leverages code from three helper files crmservicehelpers.cs, deviceidmanager.cs, myorganizationcrmsdktypes.cs from sdk\samplecode\cs\helpercode\ folder.
Code looks not so clear, especially if you have your organization name and live ID and want to connect to CRM Online. In this case you need simple straightforward code which allow you quickly access CRM services. From mentioned helper files I've used in my code deviceidmanager.cs, it is really helpful.

    class Program
    {
        static void Main(string[] args)
        {
            String userName;
            String password;
            String discoveryUrl;
            userName = <your-Live-ID>;
            password = <Live-ID-Password>;
            discoveryUrl = "https://<your-org-name>.crm.dynamics.com/XRMServices/2011/Discovery.svc";

            ClientCredentials credentials = new ClientCredentials();
            credentials.UserName.UserName = userName;
            credentials.UserName.Password = password;

            IServiceManagement<IOrganizationService> orgServiceManagement = ServiceConfigurationFactory.CreateManagement<IOrganizationService>(new Uri(discoveryUrl));

            DiscoveryServiceProxy _serviceProxy;

            using (_serviceProxy = new DiscoveryServiceProxy(new Uri(discoveryUrl), null, credentials, Microsoft.Crm.Services.Utility.DeviceIdManager.LoadOrRegisterDevice()))
            {
                IDiscoveryService service = _serviceProxy;

                #region RetrieveOrganizations Message

                // Retrieve details about all organizations discoverable via the
                // Discovery service.
                RetrieveOrganizationsRequest orgsRequest =
                    new RetrieveOrganizationsRequest()
                    {
                        AccessType = EndpointAccessType.Default,
                        Release = OrganizationRelease.Current
                    };
                RetrieveOrganizationsResponse organizations =
                    (RetrieveOrganizationsResponse)service.Execute(orgsRequest);

                // Print each organization's friendly name, unique name and URLs
                // for each of its endpoints.
                Console.WriteLine();
                Console.WriteLine("Retrieving details of each organization:");
                foreach (OrganizationDetail organization in organizations.Details)
                {
                    Console.WriteLine("Organization Name: {0}", organization.FriendlyName);
                    Console.WriteLine("Unique Name: {0}", organization.UniqueName);
                    Console.WriteLine("Endpoints:");
                    foreach (var endpoint in organization.Endpoints)
                    {
                        Console.WriteLine("  Name: {0}", endpoint.Key);
                        Console.WriteLine("  URL: {0}", endpoint.Value);
                    }
                }
                Console.WriteLine("End of listing");
                Console.WriteLine();

                #endregion RetrieveOrganizations Message

                #region RetrieveOrganization Message

                // Retrieve details about a single organization discoverable via the Discovery service.
                //
                RetrieveOrganizationRequest orgRequest =
                    new RetrieveOrganizationRequest()
                    {
                        UniqueName = organizations.Details[organizations.Details.Count - 1].UniqueName,
                        AccessType = EndpointAccessType.Default,
                        Release = OrganizationRelease.Current
                    };
                RetrieveOrganizationResponse org =
                    (RetrieveOrganizationResponse)service.Execute(orgRequest);

                // Print the organization's friendly name, unique name and URLs
                // for each of its endpoints.
                Console.WriteLine();
                Console.WriteLine("Retrieving details of specific organization:");
                Console.WriteLine("Organization Name: {0}", org.Detail.FriendlyName);
                Console.WriteLine("Unique Name: {0}", org.Detail.UniqueName);
                Console.WriteLine("Endpoints:");
                foreach (KeyValuePair<EndpointType, string> endpoint in org.Detail.Endpoints)
                {
                    Console.WriteLine("  Name: {0}", endpoint.Key);
                    Console.WriteLine("  URL: {0}", endpoint.Value);
                }
                Console.WriteLine("End of listing");
                Console.WriteLine();

                #endregion RetrieveOrganization Message
                Console.ReadLine();
            }
        }
    }


3 comments: