web
You’re offline. This is a read only version of the page.
close
Skip to main content
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

I have the same question (0)
  • Suggested answer
    Bipin D365 Profile Picture
    28,983 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

Responsible AI policies

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

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Rishabh Kanaskar Profile Picture

Rishabh Kanaskar 228

#2
Tom_Gioielli Profile Picture

Tom_Gioielli 156 Super User 2025 Season 2

#3
MVP-Daniyal Khaleel Profile Picture

MVP-Daniyal Khaleel 149

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans