Web API integration

Would you like to build modern SPA with Kentico? Use Kentico REST service! - Not enough? I see you're going to play a big game... I'm with you! Here is an idea of how to extend Kentico with Web API functionality.

This is the third post in the series of my posts describing approach for asynchronous development with Kentico: first one shows you how to configure Kentico REST service and consume it on a client and second one describes how to consume it on the server. 

As you ay know, Kentico REST service provides you with an access to literally all objects or pages within the system. But as any other technologies or approach along with pros it has some cons. What they are? The main one, in my opinion, is that you can request the object(s) of the one type only and you can't join several objects together, e.g. you can't get CMS.User joined with CMS.UserSettings or Page (Document) with a coupled data. I do not like an idea of sending two sequential request for those two objects and joining them on the client with javascript. Also, when talking about mobile application as a client, we should keep in mind the amount of the requests we can simultaneously send to a server is limited. So what are our options in this situation? The option is to join those objects on the server and return in a single response. To achieve we can either add a web service to the application or Web API. 

Web service is a good when we need to establish server-to-server communication, as it provides strongly typed objects, so we do not need to worry about serialization and deserialization things.

But when we are talking about client-server communication we would rather preffer to receive a JSON object in a response from server and this is exactly what Web API can do for us. Actually the responce type could be easily changed to whatever we might think of. By default Web API offers JsonMediaTypeFormatter and XmlMediaTypeFormatter, but you could easily implement your custom one. This is what I like about Web API and this somewhat reminds me Kentico: it consists from separate, absolutely decoupled, pieces and you can substitute almost anything in the pipeline using either out of the box components or implement one, which will meet your exclusive requirements. Another huge advantage is that Web API is extremely lightweight and, as a result, very fast.

Adding Web API to a Kentico project

To be honest with, I didn't expect adding Web API to my Kentico project to be that much easy. So my first step was adding Web API controller, which was as easy as clicking with right button on the project in Visual Studio, selecting Add New Item, selecting Web API Controller, naming it and saving. Visual Studio prompted me about saving it into App_Code folder, which is absolutely fine. By the way I added it to web site project, which I was not sure this is possible... So I can say for sure it will work with web application project. 
Web API requires some configuration to be done before running it. The least configuration is routing. This could be done by adding just a couple of lines of code to the CMS.Base.ApplicationEvents.Initialized.Execute event handler:

private void Initialized_Execute(object sender, EventArgs e)
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}"

That's it! You are ready to utilize great features of Web API to build awesome user experience with client side application.

Live example

Just recently we've got a task to build document tree on the front end, which will repeat almost 100% of what we have in CMS Tree. Of course we built it with hierarchical viewer which worked fine at the beginning, but once amount of document grew up it came slower and slower. Even with caching everything and everywhere first page visit for each user was really slow. So this was a starting point for us for searching some nice workarounds. We ended up with loading only first two levels by default and loading any other branch on demand utilizing AJAX on the client and, as you might already guessed, Web API on the server side.

Our goal was selection of documents from underneath of specified page, those belong to any of 9 page types, including some fields from coupled tables, those are published and current user is authorized to access to. I know this might sound a bit confusing, but this is easy to achieve with awesome Document Query, that Kentico implemented in the 8th version.  

So I added Web API controller and configured routing for it, like described above. Later on I implemented just a single method within controller:

public IEnumerable<TreeNodeDTO> Get(int id)
        Func<TreeNode, string> safeValue = x => ValidationHelper.GetString(x.GetValue(Constant.DOCUMENT_PDF_FIELD_NAME), "");

        WhereCondition where = new WhereCondition();
        where.WhereEquals("NodeParentID", id);

        var childNodes = DocumentHelper.GetDocuments()
            .Columns(new List<string>() {"NodeID","NodeAliasPath","NodeHasChildren","DocumentID","DocumentName","ClassName", "PdfFile"})
            .Type("Type_1", q => q.Columns("PdfFile"))
            .Type("Type_2", q => q.Columns("PdfFile"))
            .Type("Type_3", q => q.Columns("PdfFile"))
            .Type("Type_4", q => q.Columns("PdfFile"))
            .Type("Type_5", q => q.Columns("PdfFile"))
            .Type("Type_6", q => q.Columns("PdfFile"))
            .Type("Type_7", q => q.Columns("PdfFile"))
            .Type("Type_8", q => q.Columns("PdfFile"))

        var res = childNodes.ToList()
            .Select(d => new TreeNodeDTO()
                NodeID = d.NodeID,
                DocumentID = d.DocumentID,
                NodeHasChildren = d.NodeHasChildren,
                DocumentName = d.DocumentName,
                ClassName = d.ClassName,
                FileGuid = safeValue(d)

        return res;

The first statement in my method literaly joins VIew_CMS_Tree_Joined with 9 other coupled tables, filters only published, only children of particular node and checks the permissions - it sounds ridiculous and I've expected this to be awkward from performance stand point, but I was absolutely wrong - it is lighting fast. 

TreeNodeDTO is just a container to transport needed columns to the client and contains just those 6 fields you can see in the second statement. 


As you can see ASP.NET Web API works perfect in conjunction with Kentico API and results are even more amazing: the response time is minimum, page loads very fast as well as reacts very quickly on tree expanding. So we met our goal - client is happy with his product. 

In general I want to stress once again, that you can easily build modern client page apps or SPAs providing your users with the great UX using Kentico CMS. What is important here that you do not need (actually you do not even have!) to change, modify or, in worth case, screw up Kentico functionality, you can politely extend it with whatever you need.  

Please feel free to provide your thoughts on this topic or share your experience.

Host1Plus Coupon 2018
Thank you for writing this wonderful piece here. I would love to bookmark this page for future.
12/30/2017 2:04:27 PM

Marvel Contest of Champions hack
i have enjoyed this great information so much. This was really very interesting and helpful to read. I cant wait to read more from this site.
12/27/2017 7:58:41 PM

تور تایلند
Nice post. Thank you for the info. Keep it up.
12/23/2017 11:31:04 AM

you can try this out
Positive site, where did u come up with the information on this posting? I'm pleased I discovered it though, ill be checking back soon to find out what additional posts you include.
12/7/2017 12:31:28 PM

stock vectors
Your website is really cool and this is a great inspiring article.
12/4/2017 10:52:03 AM

best digital pianobest digital piano
An fascinating discussion is value comment. I think that it is best to write extra on this matter, it won’t be a taboo topic however generally people are not enough to talk on such topics. To the next. Cheers
11/25/2017 12:11:55 PM

Best Pressure Cooker
Web API is a very useful software. Thank you for describing to us how to add it to Kentico project.
11/24/2017 4:10:51 PM

voyance telephone
Your site is a plus, premium quality do the job... Most likely quite a few will probably go along with everyone whether or not they can't take the time to explain.
11/23/2017 3:12:30 PM

MusicParadisePro is a music streaming application for Android which allows listening to music in real time and it even allows you to download any number of songs from any genre for free
11/22/2017 12:00:58 PM

one verandah
Wow! This could be one of the most useful blogs we have ever come across on thesubject. Actually excellent info! I’m also an expert in this topic so I can understand your effort.
11/22/2017 10:14:09 AM

Music Promotion
What does this have to do with music and orchestra?
11/15/2017 9:10:51 PM

the best food processor
Food processors are very useful kitchen tools that help with chopping, grinding, slicing and much more. If you are looking for the best food processors, visit our website.
11/12/2017 2:00:50 PM

But if your site discusses different topics on different pages, it can be time-consuming to find the most appropriate products to advertise for each topic.
11/10/2017 1:20:51 PM

Sex enhancer for men: Herbal A natural sex enhancer for men.
10/13/2017 3:00:04 PM

complete facilities cleaning
It should be noted that whilst ordering papers for sale at paper writing service, you can get unkind attitude. In case you feel that the bureau is trying to cheat you, don't buy term paper from it.
10/7/2017 11:13:01 AM

Banks Windsor
Very useful post. This is my first time i visit here. I found so many interesting stuff in your blog especially its discussion. Really its great article. Keep it up.
9/25/2017 10:19:32 AM

voyance web
I really impressed after read this because of some quality work and informative thoughts . I just wanna say thanks for the writer and wish you all the best for coming!.
9/25/2017 9:59:29 AM

facetime download
I really impressed after read this because of some quality work and informative thoughts . I just wanna say thanks for the writer and wish you all the best for coming!.
9/25/2017 6:30:15 AM

Benefits of Lemongrass
wow, great, I was wondering how to cure acne naturally. and found your site by google, learned a lot, now i’m a bit clear. I’ve bookmark your site and also add rss. keep us updated.
9/21/2017 8:19:18 PM

male edge canada
This is really an extraordinary read for me. I have bookmarked it and I am anticipating perusing new articles. Keep doing awesome!.
9/21/2017 10:37:12 AM

phenobestin 37.5 before and after pictures
After I have observed it well, what this writes looks very interesting too. Make a good addition with more interesting info.
9/20/2017 6:08:26 AM

Natural Food
Just saying thanks will not just be sufficient, for the fantasti c lucidity in your writing. I will instantly grab your rss feed to stay informed of any updates.
9/18/2017 10:28:00 AM

free latest music maniac
music maniac is a most user-friendly app with loads of music albums, we can download unlimited songs on the music maniac
9/11/2017 4:08:38 PM

es file explorer for pc
Check The latest version of ex file explorer for pc android and ios devices
9/9/2017 11:35:50 AM

Help with Coursework
The favorable position is the point at which I'm prepared to connect to a genuine database supplier to my Web API, I ought to have the capacity to rerun my combination tests and have them act similarly. Any fizzled test will uncover any oversights or wrong presumptions I've made when coordinating with that new database supplier.
9/9/2017 8:01:54 AM

Inverse condemnation attorneys
I think you made some good points in Features also. Keep working, great job
8/8/2017 8:02:48 AM

mcse training
Cool you write, the information is very good and interesting, I'll give you a link to my site.
8/8/2017 7:50:12 AM

cyber security training
Very interesting insights, will be back for more!
8/8/2017 7:45:37 AM

comptia a+ classroom training uk
When your website or blog goes live for the first time, it is exciting. That is until you realize no one but you and your.
8/8/2017 7:37:27 AM

ccnp training london
This is just the reason why I’ve always liked your work. You have amazing writing skills and you display them in every article. Keep it going!
8/8/2017 7:31:13 AM

java training london
I like this post,And I figure that they having a great time to peruse this post,they might take a decent site to make an information,thanks for sharing it to me
8/8/2017 7:19:44 AM

excel courses
It’s good to check this kind of website. I think I would so much from you.
8/8/2017 7:16:00 AM

comptia a+ training london
You give readers a lot to think about and I appreciate that kind of writing.
8/8/2017 7:09:09 AM

Cyber security training London
I can set up my new thought from this post. It gives inside and out data. A debt of gratitude is in order for this significant data for all.
8/8/2017 6:52:21 AM

Brother pe770 Reviews
This article is pretty nicely explained article.
7/31/2017 2:58:27 PM

Han Solo Jacket Force Awakens
I have express a few of the articles on your website now, and I really like your style of blogging. I added it to my favorite’s blog site list and will be checking back soon…
7/10/2017 8:44:12 AM

Do my Coursework UK
I have experienced this article and came to think about the theme which you have shared here about the subject. Be that as it may, I don't comprehend the idea as I don't have much information around there. I think I require more illumination about the source code.
6/22/2017 7:57:46 AM

private rijksmuseum tour
I have gone through this article and came to know about the topic which you have shared here about the topic. But I don’t understand the concept as I don’t have much knowledge in this area. I think I need more clarification about the source code.
6/14/2017 11:42:01 AM

private rijksmuseum tour
I have gone through this article and came to know about the topic which you have shared here about the topic. But I don’t understand the concept as I don’t have much knowledge in this area. I think I need more clarification about the source code.
6/14/2017 11:41:57 AM

2V0-621D Exam Dumps
IT experts consider exam dumps a vital part of the preparation of 2V0-621D certification exam.
6/8/2017 7:56:58 AM

best assignment help
6/8/2017 5:55:38 AM

Negan Costume Jacket
Web APIs intregration is a mechanism that every big website allow their user they can use their website service how is it possible as we know every big website providing their services for their valuable users they can share their database with us throw by xml mechanism this the wonderful technologies its going to famous now a days its normaily use hotels engine and ecommerece website as well as other website.
how to intregration such kind APIs this is responsiblaties any developer they knwo about it how tis possible by developer.
5/25/2017 10:49:53 AM

Dissertation Topic and Plan
The majority of production Python web applications rely on several externally hosted application programming interfaces (APIs). APIs are also commonly referred to as third party services or external platforms. Examples include Twilio for messaging and voice services, Stripe for payment processing and Disqus for embedded webpage comments.

There are many articles about proper API design but best practices for integrating APIs is less commonly written about. However, this subject continuously grows in importance because APIs provide critical functionality across many implementation areas.
5/13/2017 6:10:07 AM

server recycling
You made such an interesting piece to read, giving every subject enlightenment for us to gain knowledge. Thanks for sharing the such information with us to read this...
4/10/2017 12:15:03 PM

 Security code