Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

Call API , Return Json

Posted on by Microsoft Employee

Hello Expert,

I am trying to build a plugin which calling API and return web response with type Json 

How could I catch the message in a specific tag

Here is the json response {"ResultMessage":"Enter a valid date ( from date is before First Hire date)","DocumentNo":0,"Result1":"","Result2":""}

When I am trying to throw the ResultMessage in an exception it gives me the following error 

System.Runtime.Serialization.SerializationException: Expecting element 'root' from namespace ''.. Encountered 'None'  with name '', namespace ''. 

var request = WebRequest.Create(myUri);
request.Method = WebRequestMethods.Http.Post;

var response = request.GetResponse();

var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
ResultMessage refundResponse = new ResultMessage();
DataContractJsonSerializer deserializer = new DataContractJsonSerializer(typeof(ResultMessage));
MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(responseString));
ResultMessage R = deserializer.ReadObject(ms) as ResultMessage;
refundResponse = deserializer.ReadObject(response.GetResponseStream()) as ResultMessage;

throw new InvalidPluginExecutionException(refundResponse.ResultMessage);

[DataContract]
public class ResultMessage
{
[DataMember(Name = "resultmessage")]

public string ResultMessage { get; set; }
[DataMember]
public string Result1 { get; set; }
[DataMember]
public string Result2 { get; set; }
[DataMember]
public int DocumentNo { get; set; }
}

*This post is locked for comments

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,240 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans