Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested answer

Webhook deserialization fails for datetime and some optionsets fields

(0) ShareShare
ReportReport
Posted on by 5

Hello, 

I'm trying to deserialize webhook body - RemoteExecutionContext. I use preferred DataContractJsonSerializer. Unfortunately deserialization fails for all datetime fields and some optionsets.  I've tryied to use JObject, get postImage and then use toObject<>() deserialization, that fails on some attributes value deserialization. I´ve tried Newtonsoft.Json and  System.Text.Json deserialization for RemoteExecutionContext, both fails. 

Input values in postimage in json:

Datetime:

pastedimage1666676288334v1.png

OptionSet which fails in deserialization:

pastedimage1666676425169v2.png

Code to get entity from webhook:

        public override ResponseDetail DoProcess(CrmMessageHeader header, string data)
        {
            if (header.LoadFromCrm || data == null)
                throw new ApiException("Entity data not provided");

            var crmWebhookContext = DataContractJsonSerializerHelper.Deserialize(data);
            var carnet = crmWebhookContext.GetPostImage().ToEntity();
            
            .....
        }

DataContractJsonSerializerHelper Deserialize:

public static class DataContractJsonSerializerHelper
{
    public static TObject Deserialize(string jsonString)
    {
        var settings = new JsonSerializerSettings
        {
            Converters = { new DataContractDateTimeOffsetConverter(true) },
        };

        var obj = Activator.CreateInstance();

        var ms = new MemoryStream(Encoding.Unicode.GetBytes(jsonString));

        var serializer = new DataContractJsonSerializer(obj.GetType());

        obj = (TObject)serializer.ReadObject(ms);

        ms.Close();

        return obj;
    }

RemoteExecutionContextExtension GetPostImage:

public static class RemoteExecutionContextExtensions
{
    public static Entity GetPostImage(this RemoteExecutionContext context, string name = "image")
    {
        if (!context.PostEntityImages.Contains(name))
            throw new ApiException($"CrmWebHook does not contains PostImageEntity {name}");
        return context.PostEntityImages[name];
    }

Result in entity:

pastedimage1666678506497v3.png

Result in deserialized RemoteExecutionContext:

2022_2D00_10_2D00_25_5F00_8_2D00_18_2D00_06.png

In RemoteExecutionContext are the values already deserialized wrong. We would like to use generic method for deserialization and not for every webhook type parse values from attributes. Have anyone any idea?

Thank you in advance.

Daniel

  • Suggested answer
    Bipin D365 Profile Picture
    28,981 Moderator on at
    RE: Webhook deserialization fails for datetime and some optionsets fields

    Hi,

    I remember using code from below article to deserialize RemoteExecutionContext

    dynamicsninja.blog/.../

    Once you get RemoteExecutionContext object then get post entity image and store it into Entity object

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

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Daivat Vartak (v-9davar) Profile Picture

Daivat Vartak (v-9d... 225 Super User 2025 Season 1

#2
Eugen Podkorytov Profile Picture

Eugen Podkorytov 106

#2
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 106 Most Valuable Professional

Overall leaderboard

Product updates

Dynamics 365 release plans