Monday, 14 June 2021

Console code - dynamics CRM - latest working

Console code - dynamics CRM - latest working
You need to create a azure app and enable permission to use the crm environment. 

App Id is the "azure app" id that you have created in azure. 


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

class program
{

    static void Main(string[] args)
    {
       
        string url = "https://org72e95421.crm.dynamics.com";
     
        string userName = "user@5521.onmicrosoft.com";
 
        string password = "pass@123";

        string conn = $@"
    Url = {url};
    AuthType = OAuth;
    UserName = {userName};
    Password = {password};
    AppId = 77c8a4be-1cf1-4022-8ff9-f03c9e88b3b3;     
    RedirectUri = https://localhost;
    LoginPrompt=Auto;
    RequireNewInstance = True";

        using (var svc = new CrmServiceClient(conn))
        {

 if (svc.IsReady)
            {
                var myContact = new Entity("contact");
                myContact.Attributes["lastname"] = "Test";
                myContact.Attributes["firstname"] = "ram";
                svc.Create(myContact);
                Console.WriteLine("created contact");
            }


            WhoAmIRequest request = new WhoAmIRequest();

            WhoAmIResponse response = (WhoAmIResponse)svc.Execute(request);

            Console.WriteLine("Your UserId is {0}", response.UserId);

            Console.WriteLine("Press any key to exit.");
            Console.ReadLine();
        }
    }
}







No comments:

Post a Comment

How to Call an Action through Power Automate(MS Flow)

  Introduction As Microsoft is providing us more flexibility with Power Automate (MS Flow), recently we found that now we can call the Actio...