31 May 2012

CRM Online connect to IOrganizationService

In previous post I told how to connect to Discovery Service CRM Online. Now I want give you example of code how to connect to Organization Service



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

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

            OrganizationServiceProxy _serviceProxy;
            IOrganizationService _service;
            using (_serviceProxy = new OrganizationServiceProxy(
new Uri(organizationUrl), 
null
credentials, 
Microsoft.Crm.Services.Utility.DeviceIdManager.LoadOrRegisterDevice()))
            {
                _service = (IOrganizationService)_serviceProxy;

                WhoAmIResponse response = 
(WhoAmIResponse)_service.Execute(new WhoAmIRequest());

                Console.WriteLine(response.UserId.ToString());
            }
            Console.ReadLine();
        }
    }


3 comments:

  1. Thanks!
    You have 2 equal lines:
    credentials.UserName.UserName = userName;

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete