Processing JSON within a CRM Plugin

[Originally Posted By]: https://prasadiapsara.wordpress.com/2015/11/09/processing-json-within-a-crm-plugin/

mq1                        json_logo-555px

Even though there are so many interesting and straightforward .NET libraries to process JSON  such as Json.NET, that is not going to work with MS Dynamics CRM Plugins. Because we cannot reference third party assemblies from a plugin assembly. There are some ways that you can refer a third party assembly such as ‘Assembly Merging’, but they are intense and time consuming.

So if we are to process JSON from a CRM plugin below approach can be followed.

  1. Serializing a JSON object
  • Add a Data Contract Class representing the JSON Model
[DataContract]
public class RefundReviewRequest
{
    //datamember name value indicates name of json field to which data will be serialized/from which data will be deserialized
    [DataMember(Name = "refundId")]
    public string refundId { get; set; }

    [DataMember(Name = "refundFee")]
    public string refundFee { get; set; }

    [DataMember(Name = "refundPhase")]
    public string refundPhase { get; set; }

    public RefundReviewRequest() { }
}
  • Create an object using the data contract
RefundReviewRequest refundRequest = new RefundReviewRequest
                        {
                            refundId = postImageEntity.Attributes["nxt_refundid"].ToString(),
                            refundFee = postImageEntity.Attributes["nxt_refundfee"].ToString(),
                            refundPhase = postImageEntity.Attributes["nxt_refundphase"].ToString()
                        };
  • Serialize the object created and pass it through a web stream
System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(refundRequest.GetType());
                        MemoryStream ms = new MemoryStream();
                        serializer.WriteObject(ms, refundRequest);
                        string jsonMsg = Encoding.Default.GetString(ms.ToArray());
                        var uri = new Uri('http://testURIString');
 
                        var request = WebRequest.Create(uri);
                        request.Method = WebRequestMethods.Http.Post;
                        request.ContentType = "application/json";
                        byte[] bytes = System.Text.Encoding.ASCII.GetBytes(jsonMsg.ToString());
                        request.ContentLength = bytes.Length;
 
                        System.IO.Stream os = request.GetRequestStream();
                        os.Write(bytes, 0, bytes.Length);
                        os.Close();

2. De-serializing a JSON Object

  • Add a data contract class to represent the JSON object
[DataContract]
public class RefundReviewResponse
{
    //datamember name value indicates name of json field to which data will be serialized/from which data will be deserialized
    [DataMember(Name = "refundId")]
    public string refundId { get; set; }

    [DataMember(Name = "status")]
    public string status { get; set; }

    [DataMember(Name = "message")]
    public string message { get; set; }

    public RefundReviewResponse() { }
}
  • De-serialize a JSON response and process it
using (var response = request.GetResponse())
{
    RefundReviewResponse refundResponse = new RefundReviewResponse();
    System.Runtime.Serialization.Json.DataContractJsonSerializer deserializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(refundResponse.GetType());
    refundResponse = deserializer.ReadObject(response.GetResponseStream()) as RefundReviewResponse;
    entOrderProduct.Attributes["nxt_refundreviewresponse"] = refundResponse.status;

    if (refundResponse.status != "101")
    {
       
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s