Sitecore Item Web API and Json.Net Test Drive

Sitecore recently released it’s Item Web API product and I think it will be good for the community to see some sample application. I plan a to do a 3 series blog iteration in the coming weeks, this, being the first one.

  1. Sitecore Item Web API and Json.Net Test Drive
  2. Sitecore Item Web API and Json.Net Test Drive (Part II – Strongly Typed)
  3. Sitecore Item Web API and MonoTouch (iOS development), specifically MonoTouch.Dialog – awesome, awesome framework.

Before moving on, the assumption is you have already installed and configured Sitecore’s Item Web API in your development environment. I can probably cover that as well on a separate blog. It’s really simple, really.

So now that you have Sitecore’s Item Web API installed, you are probably asking yourself what does this new tool provide? In a nutshell, it provides you 3 things:

  1. Allows client to get Sitecore items based on ID or query. For now, based on my evaluation, it supports Sitecore query and Sitecore fast query. The limitation I saw initially was it does not allow me to include query filters like [@@templatename=’Product’].
  2. Allows item manipulation (add/update/delete). This is done via the different Http verbs.
  3. Allows media uploads.

Now that we got that out of the way, let’s look at the existing code below.

  1. Lines 3 to 10 are just basic variable declarations. It gets the values from the app.config this case.
  2. Lines 12 to 19 just sets the string variable request based on the value of the query.
  3. Line 21 downloads the actual request
  4. Line 23 parses the returned json string using Json.Net. Note the usage of the dynamic keyword. Very powerful stuff.
  5. If you look at the returned json string, you can see that it contained the items array inside the result.
Code Snippet
  1. static void Main(string[] args)
  2.         {
  3.             var client = new WebClient();
  4.  
  5.             var apiUrl = getConfiguration("apiUrl");
  6.             var query = getConfiguration("query");
  7.             var sc_itemid = getConfiguration("sc_itemid");
  8.             var apiParam = getConfiguration("apiParam");
  9.             var payLoad = getConfiguration("payload");
  10.             string request = "";
  11.  
  12.             if (apiParam.Equals("query",StringComparison.OrdinalIgnoreCase))
  13.             {
  14.                 request = string.Format("{0}?{1}&{2}", apiUrl, query, payLoad);
  15.             }
  16.             else if (apiParam.Equals("id",StringComparison.OrdinalIgnoreCase))
  17.             {
  18.                 request = string.Format("{0}?{1}&{2}", apiUrl, sc_itemid, payLoad);
  19.             }
  20.  
  21.             var apiResponse = client.DownloadString(request);
  22.  
  23.             dynamic jsonResponse = JObject.Parse(apiResponse);
  24.  
  25.             if (null!=jsonResponse && jsonResponse.statusCode=="200")
  26.             {
  27.                 Console.WriteLine("Status code:" + jsonResponse.statusCode);
  28.                 Console.WriteLine("Total Count:" + jsonResponse.result.totalCount);
  29.  
  30.                 foreach (var item in jsonResponse.result.items)
  31.                 {
  32.                     Console.WriteLine(item.DisplayName + ": " + item.Path);
  33.                 }
  34.             }
  35.  
  36.             Console.WriteLine("Done.");
  37.             Console.ReadLine();
  38.         }
  39.  
  40.         private static string getConfiguration(string p)
  41.         {
  42.             return ConfigurationManager.AppSettings[p];
  43.         }

Here’s a copy of the returned json. Viewed using Chrome and Pretty Json.

image

Here’s a copy of the app.config

App.config
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <configuration>
  3.     <appSettings>
  4.         <add key="apiUrl" value="http://sitecore.local/-/item/v1"/>
  5.        
  6.         <add key="query" value="query=/sitecore/content/home/products/*"/>
  7.  
  8.         <!--
  9.             apiParam: values could be query or id
  10.         -->
  11.         <add key="apiParam" value="qUery"/>
  12.        
  13.         <add key="sc_itemid" value="sc_itemid={F29FBDBA-8DA7-4EE7-8114-07ECC25F42CF}"/>
  14.        
  15.         <!--
  16.             Payload: values could be min, content, or full
  17.         -->
  18.         <add key="payload" value="payload=min"/>
  19.        
  20.         <!-- <add key="fields" value=""/> -->
  21.     </appSettings>
  22. </configuration>

In closing, I’ve only touched the surface of Sitecore’s Item Web API. I’m sure the next releases will build upon this new framework and make it more awesome.

No Comments