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...
Suggested Answer

Convert EntityCollection to JSON

(0) ShareShare
ReportReport
Posted on by 40

Hello experts,

My requirement is - Create an application which will take GUID as input parameter and then fetch the records associated with that GUID and throw in browser in JSON format.

For e.g., I can pass an Account GUID, and the application should return all Contacts associated with this Account which are Active and added in last 1 year in JSON format. (Account/Contact is made up, actually i have to query a custom entity).

I am able to code it upto here without any issue, My code is returning exactly 17 records as it should be with those filter criteria.

EntityCollection accountcollection = _service.RetrieveMultiple(new FetchExpression(fetchaccounts)); After this line I am not sure how to convert accountcollection (CRM SDK Object) directly to JSON data and throw it in the browser or save in notepad without creating a generic class.

Can somebody please help.

Thank you.

I have the same question (0)
  • Johao Larios Profile Picture
    1,795 on at

    Hello, you can try another approach, by doing an HTTP request to the CRM: 

    Do a webapi query with something like this: 

    using (HttpClient client = new HttpClient())
    {
    client.BaseAddress = new Uri(serviceUrl);
    client.Timeout = new TimeSpan(0, 2, 0); //2 minutes
    client.DefaultRequestHeaders.Add("OData-MaxVersion", "OData-MaxVersion");
    client.DefaultRequestHeaders.Add("OData-Version", "4.0");
    client.DefaultRequestHeaders.Accept.Add(
    new MediaTypeWithQualityHeaderValue("application/json"));
    HttpRequestMessage request =
    new HttpRequestMessage(HttpMethod.Get, "/api/data/v9.0/opportunities?$select=name");
    //Set the access token
    request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", atoken);
    HttpResponseMessage response = client.SendAsync(request).Result;
    if (response.IsSuccessStatusCode)
    {
    //Get the response content and parse it.
    JObject body = JObject.Parse(response.Content.ReadAsStringAsync().Result);
    // Guid userId = (Guid)body["UserId"];
    // Console.WriteLine("Your system user ID is: {0}", userId);
    Console.WriteLine(body.ToString());
    Console.ReadLine();
    }

    With this, you will need an OAuth connection to get the token:
    https://docs.microsoft.com/en-us/powerapps/developer/common-data-service/authenticate-oauth

    Over here you have a complete example.


    Regards. 

  • Puneet Joshi Profile Picture
    40 on at

    I can not use webapi at the moment, I want to know how to convert EntityCollection to JSON.

    Please let me know.

  • Suggested answer
    Johao Larios Profile Picture
    1,795 on at

    Hello Puneet, 

    in this case what I would recommend you is create a class and navigate the accountCollection and create the json object as needed by using the generic class. 

    EnityCollectionClass:

    https://docs.microsoft.com/en-us/dotnet/api/microsoft.xrm.sdk.entitycollection?view=dynamics-general-ce-9

    regards,

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 46 Most Valuable Professional

#2
Pallavi Phade Profile Picture

Pallavi Phade 33

#3
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 28 Super User 2025 Season 2

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans