Connect Dynamics 365 in Console Application using Application Registration and Application User

 

Dynamics

URL:   https://azure.microsoft.com/en-in/get-started/azure-portal
https:  https://portal.azure.com/#home

Steps in Microsoft Azure

  1. Navigate to portal.azure.com and login.

  2. Navigate to Azure Active Directory > App Registrations > New Registration

    Step 2 in Microsoft Azure

  3. Register your Application.

    1. Enter an Application name.

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

    3. Click Register.

    Step 3 in Microsoft Azure

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

    Step 4 in Microsoft Azure

  5. Create a new Client Secret for the Application.

    1. Select Certificates & secrets > New Client Secret

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

      Step 5 in Microsoft Azure

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

    Step 6 in Microsoft Azure

  7. Grant Microsoft Dynamics permissions.

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

    2. Select the checkbox for user_impersonation.

    3. Click Add permissions.

      Step 7 in Microsoft Azure

Steps in Power Platform admin center

  1. Login to https://admin.powerplatform.microsoft.com/environments and select your environment.

  2. Under Settings, select Users + permissions >  Application users.


     
  3. In the Applications users page, select + New app user. A side menu slider will appear. Here you will:

    1. Add the App you just created from Microsoft Entra 

    2. Select the Business unit which points to your CRM
    3. Edit the Security roles and add System Administrator and System Customizer roles before finally clicking Create. 



Steps in Sitefinity

  1. Navigate to the backend of your portal site

  2. Go to Administration > Portal Connector Configuration

    Step 2 in Sitefinity

  3. Fill in the Dynamics CRM connection settings

    1. Under Connection Type select Client Secret

    2. Under Server URL, Enter the URL to your dynamics server using the appropriate format, examples of which are displayed below the field.

    3. Under Application (client) ID, paste the Application (Client) ID noted previously in the Azure Active Directory section.

    4. Under Client Secret, paste the Client Secret copied and noted previously in the Azure Active Directory section.

    Step 3 in Sitefinity

  4. Click Save & Test Connection.

  5. Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk;
using System.Web.Services.Description;
using Microsoft.Xrm.Tooling.Connector;

namespace Web_Job
{
    internal class azureCon
    {
        static string crmUrl = "https://orgcf29eb6b.crm.dynamics.com/";
        static string clientId = "d525bed6-a3cb-4851-bc26-261bcae5d28e";
        static string clientSecret = "Zkw8Q~qY2H46x7dK6vIjZFv~A6BKekOHMREc0ckK";
        static string authority = "https://login.microsoftonline.com/f5049063-937d-404d-a9b8-10028ced6a7f";
        static string connectionString = $@"
AuthType=ClientSecret;
Url={crmUrl};
Clientid={clientId};
ClientSecret={clientSecret};
Authority={authority};
RequireNewInstance = True;
";
        static void Main(string[] args)
        {
            CrmServiceClient service = new CrmServiceClient(connectionString);
            if (service.IsReady)
            {
                Console.WriteLine("connection success");

                string Query = "<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 Contacts = (EntityCollection)service.RetrieveMultiple(new FetchExpression(Query));

                foreach (Entity entity in Contacts.Entities)
                {
                    Console.WriteLine(entity["fullname"]);
                }
            }
            else
            {
                Console.WriteLine("failed");
            }
            Console.ReadLine();
        }
    }
}

Comments

Popular posts from this blog

2. On Click of case Reject button, show popup with reject reason text, once filled and submitted, reject reason field should get filled and case should be in read only

8. On selection of asset in case entity, read the project in asset, project start date and end date from project entity, set the dates on case entity from as project start date and end dates

1. In Case Entity, based on Incident Type selection, filter the case category using the category-incident master entity