Search This Blog

Friday, October 26, 2018

Connect Dynamics CRM/365 V9 with Asp.net Application c#

 using System.ServiceModel.Description;
 using Microsoft.Xrm.Sdk;

IOrganizationService organizationService = null;
        public void Connection()
        {
            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;
                // Get the URL from CRM, Navigate to Settings -> Customizations -> Developer Resources
                // Copy and Paste Organization Service Endpoint Address URL
                organizationService = (IOrganizationService)new OrganizationServiceProxy(new Uri("https://orgname.api.crm4.dynamics.com/XRMServices/2011/Organization.svc"),
                 null, clientCredentials, null);

                if (organizationService != null)
                {
                    Guid userid = ((WhoAmIResponse)organizationService.Execute(new WhoAmIRequest())).UserId;

                    if (userid != Guid.Empty)
                    {
                        Label1.Text = "Established Connection!!!";
                    }
                }
                else
                {
                   Label1.Text = "Failed to Established Connection!!!";
                }
            }
            catch (Exception ex)
            {
                Label1.Text = "Exception caught - " + ex.Message;
            }
        }