Steps: -
Video link : How to connect console application to connect Dynamics
1. Open Azure Portal
2. Login with your CRM login Credentials
3. open Microsoft Entra ID
4. click App registrations.
5. select New registration
6. After Registering the application, you will get the Application Id (Client Id)..etc save the details.
7. Select App Permissions, click on Add permission, then select Dynamics 365.
8. Grant permissions to the created permissions.
9. Select Client & secrets, then click on New client secret, and provide the appropriate details.
10. After creating a secret you should copy the secret value, otherwise you cannot copy it later.
11. Open Power portal administrator, login with your CRM credentials.
12. Select your required environment, open users.
13. select application users, then select +New app user.
14. click on "Add an app", then select the created Azure application.
15. Provide the security role as System administrator, because it has the highest privileges.
16. Now you can connect your organization from a C# code with secret credentials.
17. Copy the end point from the azure application.
Console Application 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_Jobs_1
{
internal class CrmService_Microsoft_Entra_Id
{
static string crmUrl = "https://org0ec3****.crm8.dynamics.com/"; // org url
static string clientId = "f5670056-e****-4a30-b****-137****"; // Application Id from Azure poratl
static string clientSecret = "fE58Q~mneg*******nPwPiUlhk.xVaM3"; // Secret value
static string authority = "https://login.microsoftonline.com/9c****68-***-44e8-***-cde14746112f"; // End point from azure appliaction
static string connectionString = $@"
AuthType=ClientSecret;
Url={crmUrl};
Clientid={clientId};
ClientSecret={clientSecret};
Authority={authority};
RequireNewInstance = True;
";
static void Main()
{
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");
}
}
}
}
Comments
Post a Comment