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.
- Sitecore Item Web API and Json.Net Test Drive
- Sitecore Item Web API and Json.Net Test Drive (Part II – Strongly Typed)
- 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:
- 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’].
- Allows item manipulation (add/update/delete). This is done via the different Http verbs.
- Allows media uploads.
Now that we got that out of the way, let’s look at the existing code below.
- Lines 3 to 10 are just basic variable declarations. It gets the values from the app.config this case.
- Lines 12 to 19 just sets the string variable request based on the value of the query.
- Line 21 downloads the actual request
- Line 23 parses the returned json string using Json.Net. Note the usage of the dynamic keyword. Very powerful stuff.
- If you look at the returned json string, you can see that it contained the items array inside the result.
- static void Main(string[] args)
- {
- var client = new WebClient();
- var apiUrl = getConfiguration("apiUrl");
- var query = getConfiguration("query");
- var sc_itemid = getConfiguration("sc_itemid");
- var apiParam = getConfiguration("apiParam");
- var payLoad = getConfiguration("payload");
- string request = "";
- if (apiParam.Equals("query",StringComparison.OrdinalIgnoreCase))
- {
- request = string.Format("{0}?{1}&{2}", apiUrl, query, payLoad);
- }
- else if (apiParam.Equals("id",StringComparison.OrdinalIgnoreCase))
- {
- request = string.Format("{0}?{1}&{2}", apiUrl, sc_itemid, payLoad);
- }
- var apiResponse = client.DownloadString(request);
- dynamic jsonResponse = JObject.Parse(apiResponse);
- if (null!=jsonResponse && jsonResponse.statusCode=="200")
- {
- Console.WriteLine("Status code:" + jsonResponse.statusCode);
- Console.WriteLine("Total Count:" + jsonResponse.result.totalCount);
- foreach (var item in jsonResponse.result.items)
- {
- Console.WriteLine(item.DisplayName + ": " + item.Path);
- }
- }
- Console.WriteLine("Done.");
- Console.ReadLine();
- }
- private static string getConfiguration(string p)
- {
- return ConfigurationManager.AppSettings[p];
- }
Here’s a copy of the returned json. Viewed using Chrome and Pretty Json.
Here’s a copy of the app.config
- <?xml version="1.0" encoding="utf-8" ?>
- <configuration>
- <appSettings>
- <add key="apiUrl" value="http://sitecore.local/-/item/v1"/>
- <add key="query" value="query=/sitecore/content/home/products/*"/>
- <!--
- apiParam: values could be query or id
- -->
- <add key="apiParam" value="qUery"/>
- <add key="sc_itemid" value="sc_itemid={F29FBDBA-8DA7-4EE7-8114-07ECC25F42CF}"/>
- <!--
- Payload: values could be min, content, or full
- -->
- <add key="payload" value="payload=min"/>
- <!-- <add key="fields" value=""/> -->
- </appSettings>
- </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.