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 :
Customer experience | Sales, Customer Insights,...
Unanswered

RemoteExecutionContext - CRM 365 to service bus - deserialising with json.net or DataContractJsonSerializer date format in json string

(2) ShareShare
ReportReport
Posted on by 35

My scenario is that I reading messages off a service bus that a plugin or OOB connector from CRM 365 adds a json object to a service bus.

Trying to use json.net to deserialize the message I encounter an issue:

Newtonsoft.Json.JsonSerializationException: 'Cannot create and populate list type Microsoft.Xrm.Sdk.KeyAttributeCollection. Path 'OwningExtension.KeyAttributes', line 1, position 7017.'

So instead I have resorted to using DataContractJsonSerializer. With this I have an issue with dates when trying to map to an entity and early bound classes.

The service bus message is of type RemoteExecutionContext - but when I deserialize this message the date formats are in the following:

 "key": "createdon",
 "value": "/Date(1633688867000)/"

I would like it in the DateTime format before converting to an account model for instance otherwise dates will not work correctly and the mapping will fail.

Any ideas on how to solve this would be appreciated

I have the same question (0)
  • YorgunKelebek Profile Picture
    10 on at

    Try the suggestion in this tread:

    community.dynamics.com/.../parse-datetime-in-plugin-execution-context-json

    Hope it helps

  • thenerdynerd Profile Picture
    on at

    Hi there, thanks for replying - seems to have stumped a few -

    I did manage to find something like this on stackoverflow but requires me to go through each field - or to code manually for create dates, edit dates or any other dates needed in code.

    Would be good if there was a function that would allow for dates to be formatted as needed when I convert to entity - as when casting "Target" toEntity these dates are not converted correctly.

    I was just wondering if there was a way to do it that doesn't appear to be so "manual"?

  • YorgunKelebek Profile Picture
    10 on at

    Hi

    Maybe you can try the settings for DataContractJsonSerializer https://docs.microsoft.com/en-us/dotnet/api/system.runtime.serialization.json.datacontractjsonserializer.datetimeformat?view=net-5.0

    DataContractJsonSerializerSettings settings = new DataContractJsonSerializerSettings
    {
        DateTimeFormat = new DateTimeFormat("yyyy-MM-ddTHH:mm:ss.fffK")
    };

    But I am not sure if this would work with RemoteExecutionContext - worth a try though...

    Also have a look at this, there may be something that could help: stackoverflow.com/.../why-do-datacontractjsonserializer-and-json-net-serialization-of-datetimeoffset-p

  • thenerdynerd Profile Picture
    on at

    Hi thanks for the reply, apologies, it's taken me awhile to test this.

    I am getting the following error:

    There was an error deserializing the object of type Microsoft.Xrm.Sdk.RemoteExecutionContext. String '/Date(1633972067000 0000)/' was not recognized as a valid DateTime.'

    This is how I am using it:

    var settings = new DataContractJsonSerializerSettings
    {
     DateTimeFormat = new DateTimeFormat("yyyy-MM-ddTHH:mm:ss.fffK")
    };
    var stream                 = new MemoryStream(messageBody);
    var remoteExecutionContext = 
     (RemoteExecutionContext)new DataContractJsonSerializer(typeof(RemoteExecutionContext), settings).ReadObject(stream);
    

    The only sure fire way to do this is to do as I mentioned above:

    Add all attributes to a list and convert the attributes I need converting to a date time field as you pasted above in the thread. This works for my purposes but feel like there has to be a better way!

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 > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 170 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 61

#3
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 52 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans