web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
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,985 Moderator on at

    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

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Nagaraju_Matta Profile Picture

Nagaraju_Matta 121

#2
ManoVerse Profile Picture

ManoVerse 73 Super User 2026 Season 1

#3
Abhilash Warrier Profile Picture

Abhilash Warrier 66 Super User 2026 Season 1

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans