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 CRM (Archived)

Deserialize JSON to entity?

(0) ShareShare
ReportReport
Posted on by

Using a webhook, I am able to setup a post image call to do a POST of the entity when the entity in CRM gets updated.

I see the JSON but now on my API, I want to deserialize the JSON to a concrete object of that type - the Entity type.

On the Web API I have, when using Newtonsoft's JSON to deserialize, it does not successfully deserialize it (no errors).

But I can get the JObject, which is not what I want.

How can I do this?

Or if possible, how can I get the entity type (in this case, contact) from the CRM Service by getting it by id so I can get all the data for that contact?

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Radu Chiribelea Profile Picture
    6,667 on at

    Have you tried to use the CrmSvcUtil to generate early bound classes for your CRM entities? Does it work afterwards to deserialize the object to the given entity type?

    See here details on how to use the svcutil tool: docs.microsoft.com/.../create-early-bound-entity-classes-code-generation-tool

  • Community Member Profile Picture
    on at

    I will try this, but what about custom entities or custom attributes for an entity like contact?

  • Community Member Profile Picture
    on at

    Generating the classes is around 20MB, which makes sense but how best to reduce this?

  • Community Member Profile Picture
    on at

    So trying what you suggested, I still get an error about an unexpected carriage return (\r)

    Here is the code I am using to deserialize to the entity:

    public static Models.Contact DeserializeJsonString(string jsonString)
            {
                //create an instance of generic type object
                Models.Contact obj = new Models.Contact();
    
                var ms = new MemoryStream(Encoding.Unicode.GetBytes(jsonString));
                System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(obj.GetType());
                obj = (Models.Contact)serializer.ReadObject(ms);
                ms.Close();
                return obj;
            }


    At the line:

    obj = (Models.contact)serializer.ReadObject(ms);

    I get this error:

    System.Runtime.Serialization.SerializationException: 'There was an error deserializing the object of type PoC.Dynamics.Sync.Models.Contact. Encountered invalid character ''.'

    And the json is sent from the webhooks in CRM. Post Image.

  • Radu Chiribelea Profile Picture
    6,667 on at

    I think you need to generate your own classes for the CRM entities. I've just remembered, the name of the fields returned by the WEB API is different than what you get when calling the SOAP endpoint (Example: SOAL customerid vs WEB API _customerid_value) so the CrmSvcUtil i think it won't help in this case.

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 CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans