Friday 30 August 2019

simple console

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xrm.Sdk;
using System.ServiceModel.Description;
using System.Net;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Crm.Sdk.Messages;


namespace ConnectingtoCRM
{
    class Program
    {
        static void Main(string[] args)
        {
          //  ClientCredentials credentials = new ClientCredentials();
          //  credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;
          //  Uri OrganizationUri = new Uri("http://smdev-crm/SulekhaCRM/XRMServices/2011/Organization.svc");
          //  Uri HomeRealmUri = null;
          ////  using (Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy serviceProxy = new Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy(OrganizationUri, HomeRealmUri, credentials, null)) ;
            // Microsoft.Xrm.Sdk.IOrganizationService service = (Microsoft.Xrm.Sdk.IOrganizationService)serviceProxy;

            IOrganizationService organizationService = null;

            String username = "damodaram@demotrainingb.onmicrosoft.com";//eg: abc@xyz.onmicrosoft.com
            String password = "damu@123";//eg: password@123

            // Get the URL from CRM, Navigate to Settings -> Customizations -> Developer Resources
            // Copy and Paste Organization Service Endpoint Address URL
            String url = "https://demotrainingb.crm8.dynamics.com/main.aspx"; //eg: https://<yourorganisationname>.api.crm8.dynamics.com/XRMServices/2011/Organization.svc
            try
            {
                ClientCredentials clientCredentials = new ClientCredentials();
                clientCredentials.UserName.UserName = username;
                clientCredentials.UserName.Password = password;

                // For Dynamics 365 Customer Engagement V9.X, set Security Protocol as TLS12
                // ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

                organizationService = (IOrganizationService)new OrganizationServiceProxy(new Uri(url), null, clientCredentials, null);

                if (organizationService != null)
                {
                    Guid gOrgId = ((WhoAmIResponse)organizationService.Execute(new WhoAmIRequest())).OrganizationId;
                    if (gOrgId != Guid.Empty)
                    {
                        Console.WriteLine("Connection Established Successfully...");
                    }
                }
                else
                {
                    Console.WriteLine("Failed to Established Connection!!!");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception occured - " + ex.Message);
            }
            //return organizationService;

        }
    }
}