Monday, 28 May 2018

Console Application for Dynamics CRM connection


make sure to use the SDK  libraries


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

namespace Crm2
{
    public class Class1
    {
        static IOrganizationService _service;
        static void Main(string[] args)
        {
            ConnectToMSCRM("crm@midjune.onmicrosoft.com", "dora@123", "https://midjune.api.crm.dynamics.com/XRMServices/2011/Organization.svc");
            Guid userid = ((WhoAmIResponse)_service.Execute(new WhoAmIRequest())).UserId;
            if (userid != Guid.Empty)
            {

             
                Console.WriteLine("Connection Established Successfully");
                Console.ReadKey();
            }
        }
        public static void ConnectToMSCRM(string UserName, string Password, string SoapOrgServiceUri)
        {
            try
            {
                ClientCredentials credentials = new ClientCredentials();
                credentials.UserName.UserName = UserName;
                credentials.UserName.Password = Password;
                Uri serviceUri = new Uri(SoapOrgServiceUri);
                OrganizationServiceProxy proxy = new OrganizationServiceProxy(serviceUri, null, credentials, null);
                proxy.EnableProxyTypes();
                _service = (IOrganizationService)proxy;

                //Entity ent = new Entity("annotation");
                ////ent.Attributes["name"] = "Created from Console App4";
                ////_service.Create(ent);


             
                //// Create a column set to define which attributes should be retrieved.
                //ColumnSet attributes = new ColumnSet(new string[] { "name", "ownerid" });

                //// Retrieve the account and its name and ownerid attributes.
                //account = _service.Retrieve(account.LogicalName, _accountId, attributes);
                //Console.Write("retrieved, ");

             

            }
            catch (Exception ex)
            {
                Console.WriteLine("Error while connecting to CRM " + ex.Message);
                Console.ReadKey();
            }
        }
    }
}




using web-api




using Microsoft.Crm.Sdk.Messages;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Tooling.Connector;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;

namespace CSharpConsole2
{
    class Program
    {
        private static CrmServiceClient _client;

        public static void Main(string[] args)
        {

            using (_client = new CrmServiceClient(ConfigurationManager.ConnectionStrings["CRMConnectionString"].ConnectionString))
            {
                string resource = "https://junealdous.crm8.dynamics.com";
                string contact1Uri = "https://junealdous.api.crm8.dynamics.com/api/data/v9.0/invoices(14102aa1-d085-e811-a962-000d3af03d2e)";


                // TODO Substitute your app registration values that can be obtained after you

                // register the app in Active Directory on the Microsoft Azure portal.

                string clientId1 = "12345683-bd7a-45bc-aaac-480c2d9212d4";

                string redirectUrl = "https://some.api.crm8.dynamics.com/api/data/v9.0/";





                // Authenticate the registered application with Azure Active Directory.

                AuthenticationContext authContext1 =

                    new AuthenticationContext("https://login.windows.net/common", false);

                AuthenticationResult result1 = authContext1.AcquireToken(resource, clientId1, new

                                                                       Uri(redirectUrl));



                using (System.Net.Http.HttpClient httpClient = new HttpClient())

                {

                    httpClient.Timeout = new TimeSpan(0, 2, 0);  // 2 minutes

                    httpClient.DefaultRequestHeaders.Authorization =

                        new AuthenticationHeaderValue("Bearer", result1.AccessToken);

                    httpClient.BaseAddress = new Uri(redirectUrl);

                    var response = httpClient.GetAsync("accounts?$select=name").Result;

                    if (response.IsSuccessStatusCode)

                    {

                        var accounts = response.Content.ReadAsStringAsync().Result;

                     JObject contact1Update = new JObject();
        //contact1Update.Add("invoiceid", new Guid("14102aa1-d085-e811-a962-000d3af03d2e"));
        contact1Update.Add("overriddencreatedon", DateTime.Now);
                contact1Update.Add("name", "Test1233");
                //contact1Update.Add("description", "Assignment to-be-determined");

                HttpRequestMessage updateRequest2 = new HttpRequestMessage(
                   new HttpMethod("PATCH"), contact1Uri);
     
                    updateRequest2.Content = new StringContent(contact1Update.ToString(),
                    Encoding.UTF8, "application/json");
        Task<HttpResponseMessage> response1 = httpClient.SendAsync(updateRequest2);
                    if (response1.IsCompleted)
                {
               
                }


}

                }



            }


        }
    }
}

No comments:

Post a Comment

Power Apps Drag & Drop Kanban code sample

  Introduction: The Kanban Board App Developers at a software company use the Kanban board to show their progress on development tasks and b...