A Practical Guide to SharePoint 2013

A Practical Guide to SharePoint 2013
A Practical Guide to SharePoint 2013 - Book by Saifullah Shafiq

Monday, February 10, 2014

JSON-Patch Using .NET

 A Practical Guide to SharePoint 2013

A JSON Patch document is a JSON (RFC4627) document that represents an array of objects. Each object represents a single operation to be applied to the target JSON document. Read more about JSON Patch on the IETF (Internet Engineering Task Force) web site:

http://tools.ietf.org/html/rfc6902

Implementing cURL API calls in .NET was not a straight forward procedure up till now. In the absence of native support in .NET, developers were forced to use third-party libraries but Microsoft has added new objects in .NET Framework 4.5 that allow you to make calls inside .NET without having to use third-party libraries. Especially when it comes to using JSON Patch operations, the third-party libraries are still not ready for use in .NET projects that require JSON Patch operations as these libraries do not support patch operations. Popular .NET REST Clients are as following:

Hammock - Development discontinued!
RestSharp
Ramone

Hammock has been the most popular Rest client but it has been discontinued. RestSharp is another popular library but it does not support JSON Patch operations. Ramone is the most recent addition in the list of .NET libraries and although it claims to support JSON Patch operations, there is no practical example available. I personally tried to implement JSON Patch using this library but did not succeed. I am sure I could have made it work by spending some more time on it but that was not my intention. The goal was to find a library that worked out of the box for implementing JSON Patch. Finally, I had to write my own code using new HTTP web objects introduced by the Microsoft. HttpWebRequest class provides an HTTP-specific implementation of the WebRequest class. The HttpWebRequest class provides support for the properties and methods defined in WebRequest and for additional properties and methods that enable the user to interact directly with servers using HTTP. WebRequest class makes a request to a Uniform Resource Identifier (URI). WebRequest is the abstract base class for the .NET framework's request/response model for accessing data from the Internet. Requests are sent from an application to a particular URI, such as a web page on a server. You can read more about these objects on MSDN (See links inline). Below is an implementation of JSON Patch operations in .NET using HttpWebRequest and HttpWebResponse objects.

Following code reads information from remote server using HttpWebRequest and HttpWebResponse:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.server.com/2.0/files");

request.Method = "GET";
request.ContentType = "application/json-patch";

request.Headers.Add("Authorization", "Bearer " + strAccessToken); //replace strAccessToken with AccessToken

HttpWebResponse resp = (HttpWebResponse)request.GetResponse();

Encoding enc = System.Text.Encoding.GetEncoding(1252);
StreamReader respStream = new StreamReader(resp.GetResponseStream(), enc);

string response = respStream.ReadToEnd();

respStream.Close();
resp.Close();

Note: The URL used above is imaginary but the code has been tested and found to work perfectly. It reads information from remote endpoint. Endpoint returns JSON document.

Following code writes information to the remote server:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.server.com/2.0/files");

request.Method = "PUT";
request.ContentType = "application/json-patch+json";

request.Headers.Add("Authorization", "Bearer " + strAccessToken); //replace strAccessToken with actual token

string body = @"[{""op"": ""add"", ""path"": ""/FirstName"", ""value"": ""billy""}, {""op"": ""add"", ""path"": ""/LastName"", ""value"": ""bowden""}]";

ASCIIEncoding encoding = new ASCIIEncoding();
byte[] byte1 = encoding.GetBytes(body);

request.ContentLength = byte1.Length;

Stream newStream = request.GetRequestStream();
newStream.Write(byte1, 0, byte1.Length);

HttpWebResponse resp = (HttpWebResponse)request.GetResponse();

Encoding enc = System.Text.Encoding.GetEncoding(1252);
StreamReader respStream = new StreamReader(resp.GetResponseStream(), enc);

string response = respStream.ReadToEnd();

newStream.Close();

respStream.Close();
resp.Close();

This codes write metadata to the remote file and returns the operation status. I have used PUT method. Content type used is "application/json-patch=json". Operation has been added in the body. To learn more about what to pass in the body and how, read RFC document on json patch operations.

First parameter is the operation name (represented by "op" and value is "add"). Second parameter is path (represented by "path"). Path value must start with a leading slash "/". Third parameter is the "value" (represented by "value") and this parameter contains the actual value that you want applied to the remote object.

Hope you will find this information useful. Since RFC6902 is a new standard, there are very few blogs on this topic. If you have questions, feel free to get in touch with me.

No comments:

Post a Comment