Set up Client Secret Connection to Dynamics

 Steps in Microsoft Azure:

·         Navigate to portal.azure.com and login.



·         Navigate to Azure Active Directory > App Registrations > New Registration

 


 

Register your Application.

a.      Enter an Application name.

b.      Select which account types you wish to access this API.

c.       Click Register.

 


Copy the Application (client) ID and note it for later use.


 

 

Create a new Client Secret for the Application.

 

Select Certificates & secrets > New Client Secret

 

Enter a description, select an expiry duration and click Add.




Click on Certificates and Secrets and click on New Client secret

Copy the client secret and note it for later use. Make sure to copy the Value field, as below.


Then below window will opened

Here give the description name and click on Add.



After adding the client secret Note down the value


Grant Microsoft Dynamics permissions.

a.      Select API Permissions > Add a Permission > Dynamics CRM

b.      Select the checkbox for userimpersonation.

c.       Click Add permissions.

 


 

Steps in Power Platform admin center

Login to https://admin.powerplatform.microsoft.com/

Environments and select your environment( sales trail)

 


 

After click on sales trail then below window will open

Here choose see all at Users area



Choose app users Lists


Click on New app user


 

After clicking on New app user below window will open

·         Here click on Add an app]and choose app

·         Select business unit

·         Give security roles

 

 


In the Azure Portal click on Endpoint and copy the authorization end point(This will be used in code)


Code:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using Microsoft.PowerPlatform.Dataverse.Client;

using Microsoft.Crm.Sdk.Messages;

using Microsoft.Xrm.Sdk;

using Microsoft.Xrm.Tooling.Connector;

using Microsoft.Xrm.Sdk.Query;

 

namespace Webapi

{

    class Program

    {

        static string url = "https://orge19f62cf.crm8.dynamics.com/main.aspx?";

        static string clientId = "4f6f4279-a5fb-42c4-90a8-20949c9b19d0";

        static string secretId = "VWh8Q~~IWnWJZKeNlbjpp9~05zAOBeZgwNMgwduL";

        static string authority = "https://login.microsoftonline.com/a9ab94dd-336e-48ee-b7db-e7bee371e8cf";

        static string connectionString = $@"

        AuthType =ClientSecret;

        Url ={url};

        ClientId ={clientId};

       ClientSecret= {secretId};

       Authority={authority};

        RequireNewInstance = True";

        static void Main(string[] args)

        {

            CrmServiceClient Connect = new CrmServiceClient(connectionString);

            {

                if (Connect.IsReady)

                {          

                    string contactFetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +

 "<entity name='contact'>" +

 "<attribute name='fullname'/>" +

 "<attribute name='telephone1'/>" +

 "<attribute name='contactid'/>" +

 "<order attribute='fullname' descending='false'/>" +

 "</entity>" +

 "</fetch>";

                    EntityCollection contact = Connect.RetrieveMultiple(new FetchExpression(contactFetchXml));

                    if (contact.Entities.Count > 0)

                    {

                        foreach (Entity entity in contact.Entities)

                        {

                            string name = entity.Contains("fullname") ? (string)entity.Attributes["fullname"] : null;

                            string phonenum = entity.Contains("telephone1") ? (string)entity.Attributes["telephone1"] : null;

                            Console.WriteLine(name + " ---" + "Phone num: " + phonenum + "..." + "id: " + entity.Id);

                        }

                    }

                    Console.ReadLine();

 

                }

            }

        }

    }

}

Output: Execute above code then you can retrieve all contact records.

Comments

Popular posts from this blog

SSRS User Story-5: Create a SSRS report with student name, DOB, Date of Admission, parent details. Schedule this report, every day evening 6 pm it need to run and share with managers. (use power automate)

SSRS User Story-8: Create a SSRS report on the Opportunity entity, show the source lead and campaign details.

SSRS User Story 3: In order entity create a report look like mentioned below, it should show the address of selected customer and products dynamically.