web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Unanswered

connecting to dataverse using resource url and access token

(1) ShareShare
ReportReport
Posted on by 2
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Tooling.Connector;
using System;
using Microsoft.Identity.Client;
namespace Web_Jobs
{
    internal class Microsoft_Identity
    {
        private static string GetAccessToken(string clientId, string clientSecret, string tenantId, string resource)
        {
            var authority = $"https://login.microsoftonline.com/{tenantId}";
            var app = ConfidentialClientApplicationBuilder.Create(clientId)
                .WithClientSecret(clientSecret)
                .WithAuthority(new Uri(authority))
                .Build();
            var scopes = new string[] { $"{resource}/.default" };
            var result = app.AcquireTokenForClient(scopes).ExecuteAsync();
            return result.ToString();
        }
        private static void Run()
        {
            try
            {
                var clientId = "";
                var clientSecret = "";
                var tenantId = "";
                var resource = "";

                var accessToken = GetAccessToken(clientId, clientSecret, tenantId, resource);
                // Ensure the access token is successfully acquired.
                if (string.IsNullOrEmpty(accessToken))
                {
                    Console.WriteLine("Failed to acquire access token.");
                    return;
                }
                string connectionString = $"AuthType=OAuth;Url={resource};AccessToken={accessToken};";
                Console.WriteLine("*** Establishing connection to CRM... ***");
                CrmServiceClient client = new CrmServiceClient(connectionString);
                if (!client.IsReady)
                {
                    Console.WriteLine(client.LastCrmException.Message);
                }
                else
                {
                    Console.WriteLine("\n*** Connection Successful! ***");
                    // Perform client operations here.
                    // retrieving using fetch XML
                    var fetchXML = "<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'/>" +
                                   "<filter type='and'>" +
                                   "<condition attribute='parentcustomerid' operator='eq' uitype='account' value='(83883308-7ad5-ea11-a813-000d3a33f3b4)'/>" +
                                   "</filter>" +
                                   "</entity>" +
                                   "</fetch>";
                    EntityCollection contactRecords = client.RetrieveMultiple(new FetchExpression(fetchXML));
                    if (contactRecords != null)
                    {
                        foreach (var contactRecord in contactRecords.Entities)
                        {
                            contactRecord["address1_city"] = "this is from console";
                            client.Update(contactRecord);
                            Console.WriteLine(contactRecord.Attributes["fullname"]);
                        }
                    }
                    Console.WriteLine("\nPress any key to exit...");
                    Console.ReadKey(true);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        static void Main(string[] args)
        {
            Run();
        }
    }
}
 
I have the same question (0)

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
Martin Dráb Profile Picture

Martin Dráb 62 Most Valuable Professional

#2
#ManoVerse Profile Picture

#ManoVerse 57

#3
Pallavi Phade Profile Picture

Pallavi Phade 49

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans