Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Suggested answer

Pulling changes using "Prefer: odata.track-changes"

(0) ShareShare
ReportReport
Posted on by 10

Used this article as reference:

docs.microsoft.com/.../use-change-tracking-synchronize-data-external-systems

Tried this implementation in C# (fragment shown here):

.......

            string d365Entity = _D365BaseAddress + "data/Projects?cross-company=true&$select=ProjectID,ProjectName";

            var request = new HttpRequestMessage(HttpMethod.Get, Uri.EscapeUriString(string.Format(d365Entity)));

            request.Headers.Add("Prefer", "odata.track-changes");

            httpClient.DefaultRequestHeaders.Add("Cache-Control", "no-cache");

            httpClient.DefaultRequestHeaders.Add("OData-Version", "4.0");

            HttpResponseMessage response = await httpClient.SendAsync(request);

            if (response.IsSuccessStatusCode)

            {

                string jsonResult = await response.Content.ReadAsStringAsync();

            }

...........

Got this jsonResult back, which is not what I expected:

{
"@odata.context": "https://??????.sandbox.operations.dynamics.com/data/$metadata#Projects(ProjectID,ProjectName)",
"value": [
{
"@odata.etag": "W/\"JzEwOTU0ODc0MjIsNTYzNzxxxxxxxxxx\"",
"ProjectID": "101-0000XX",
"ProjectName": "Important project"
},
{
"@odata.etag": "W/\"Jzc2MjUxNzE5Miw1NjM3xxxxxxxxxxx\"",
"ProjectID": "101-000011-YY",
"ProjectName": "Not so important"
}
]
}

Questions:

  • Is Prefer: odata.track-changes supported by D365?
  • If so, what am I doing wrong?

/EirikS

  • Community member Profile Picture
    Community member 20 on at
    Pulling changes using "Prefer: odata.track-changes"
    Eirik, did you ever solve this problem?   I'm getting the same thing on D365 Fin Ops.
  • EirikS Profile Picture
    EirikS 10 on at
    RE: Pulling changes using "Prefer: odata.track-changes"

    Hi Steve,

    thank you for the response.

    I see you access CRM, with the crm.dynamics.com/api/data/v9.1/ address, whilst I use xxx.sandbox.operations.dynamics.com/data/

    I am accessing data in Financial/Operations module, not the CRM. Maybe this is why I have a problem?

    /eirik

  • Suggested answer
    RE: Pulling changes using "Prefer: odata.track-changes"

    Hi EirikS,

    I tested with the following code:

            static void Main(string[] args)
            {
                var accounts = CrmRequest(
                    HttpMethod.Get,
                    "https://xxx.crm.dynamics.com/api/data/v9.1/accounts?$select=name").Result.Content.ReadAsStringAsync()
                Console.WriteLine(accounts.Result);
            }
    
            public static async Task AccessTokenGenerator()
            {
                string clientId = "xxxx"; // Your Azure AD Application ID  
                string clientSecret = "xxxx"; // Client secret generated in your App  
                string authority = "https://login.microsoftonline.com/xxxx"; // Azure AD App Tenant ID  
                string resourceUrl = "https://xxx.crm.dynamics.com/"; // Your Dynamics 365 Organization URL  
    
                var credentials = new ClientCredential(clientId, clientSecret);
                var authContext = new Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext(authority);
                var result = await authContext.AcquireTokenAsync(resourceUrl, credentials);
                return result.AccessToken;
            }
    
            public static async Task CrmRequest(HttpMethod httpMethod, string requestUri, string body = null)
            {
                // Acquiring Access Token  
                var accessToken = await AccessTokenGenerator();
    
                var client = new HttpClient();
                var message = new HttpRequestMessage(httpMethod, requestUri);
    
                // OData related headers  
                message.Headers.Add("OData-MaxVersion", "4.0");
                message.Headers.Add("OData-Version", "4.0");
                message.Headers.Add("Prefer", "odata.track-changes");
    
                // Passing AccessToken in Authentication header  
                message.Headers.Add("Authorization", $"Bearer {accessToken}");
    
                // Adding body content in HTTP request   
                if (body != null)
                    message.Content = new StringContent(body, UnicodeEncoding.UTF8, "application/json");
    
                return await client.SendAsync(message);
            }

    And it can get the below data:

    {
       "@odata.context": "https://xxx.crm.dynamics.com/api/data/v9.1/$metadata#accounts(name)",
       "@odata.deltaLink": "https://xxx.crm.dynamics.com/api/data/v9.1/accounts?$select=name&$deltatoken=8020507!11/18/2021 08:30:07",
       "value": [
          {
             "@odata.etag": "W/\"2819116\"",
             "name": "Best o' Sales",
             "accountid": "GUID"
          }
    	  ......
       ]
    }

    You could have a try.

  • EirikS Profile Picture
    EirikS 10 on at
    RE: Pulling changes using "Prefer: odata.track-changes"

    Forgot to mention that "Change tracking" has been configured on the Entity used in the test:

    pastedimage1637158453212v1.png

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,430 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans