Find a post...

DNN-Connect Blogs

New in 2sxc 7: #4 Using Visual Query with WebAPI

This is a part of my series The New Features of 2sxc 7 and introduces you to using the Visual Query together with WebAPI

There are many ways to consume Query data - the most powerful way to give use it in JavaScript is to deliver it as JSON - which can be done easily in WebAPI. Here are the three ways to use queries in WebAPI:

  1. Retrieve the data for some C# work inside WebAPI
  2. Get a Query and just serialize the entire query with all streams into JSON
  3. Just serialize some streams (not all) of a query to JSON
  4. Serialize one single content-item from a query into JSON
  5. Manually serialize something 

Method 1: Retrieve the data for code-use in WebAPI

Web-APIs are always written in C# - and the object App.Query[...] is also available there. So you can always access it through that, like:

var query = App.Query["All Active Categories"];

var categories = App.Query["All Active Categories"]["Categories"];

Typically it's more fun to work with dynamic objects, so you would usually write:

var categories = AsDynamic(App.Query["All Active Categories"]["Categories"]);

Method 2: Deliver an entire Query to JSON

For most cases when you just want to deliver the data as-is to your client, you can use our new serializers - like this:

    
    
    
    public dynamic All()
    {
        return Sxc.Serializer.Prepare(App.Query); 

} 

Method 3: Deliver one stream from a Query to JSON

If you just want one stream - add that stream name as well:

    
    
    
    public dynamic Categories()
    {
        // The following line demonstrates how we return all categories from the query, already sorted
        return Sxc.Serializer.Prepare(App.Query); 

}

BTW: You can also add more than one custom stream - so if your Query delivers 7 streams and you just want to deliver 2 of the streams in JSON, do this:

        return Sxc.Serializer.Prepare(App.Query, "Categories, Top10"); 


Method 4: Serialize a single item

        return Sxc.Serializer.Prepare(App.Query["Default"].List.FirstOrDefault());

Method 5: Manually Serialize Something

Often you will have large objects (like Addresses with 20+ fields) but only want to serialize some fields. There are 2 ways to do this:

  1. Create a query which delivers fewer attributes using the Attribute-Filter (not explained here)
  2. Use an anonymous object in your WebAPI (see next example)

 

    
    
    
    public dynamic Categories()
    {
        // And this demonstrates how we would return a list of assembled objects with attributes as we would want
        return from c in AsDynamic(App.Query) select new {
           Id = c.EntityId,
           Title = c.EntityTitle
        }; 

}

A Note on Query Parameters when using WebAPI

Since a Query can contain any kind of parameters like or 97 or even these are automatically resolved by the Query-Engine whenever you access the query. 

Just make sure that you are using the correct URL-Parameters in the WebAPI-call as they are expected in the Query. A typical mistake would be to have in your Visual Query, but use an AJAX-call placing the Sort-Direction in the post (instead of the url). 

A Note on a fully automatic JSON-Interface

It would be nice, if actually all Visual-Queries would be available as JSON automatically, without the need for a WebAPI-Interface. The reason this is missing is for security reasons. In WebAPI you can define security requirements for each call - but the Visual Query Designer doesn't offer security settings yet. Once we add that (probably in 2sxc 7.3) we will also create a fully automatic WebAPI-interface which won't require any more coding to retrieve data in JSON. 

Have Fun! 

With love from Switzerland,

Daniel 

Daniel Mettler grew up in the jungles of Indonesia and is founder and CEO of 2sic internet solutions in Switzerland and Liechtenstein, an 20-head web specialist with over 600 DNN projects since 1999. He is also chief architect of 2sxc (2SexyContent - see forge), an open source module for creating attractive content and DNN Apps.

Daniel Mettler learned programming with the bible translation computer of his parents, deep in the jungles of Indonesia. Since he was only 12 years old at that time and the BIOS only had a version of BASICA, that's what got him started. With 16 he went back to Switzerland and learned German and basic city-survival skills. Equipped with this know-how he founded 2sic internet solutions in 1999 which was focused on web solutions on the Microsoft platform. After a few self-developed CMSs 2sic switched to DNN in 2003 and has been one of the largest partners (17 employees, 700+ projects) in Europe. Daniel is also the chief architect behind the open source 2sxc, a strong promoter of standardization (boostrap, patterns, AngularJS, checklists, etc.) and loves to eat anything - dead or alive. His motto: if the natives eat it, it game.
Comment(s)

Hosting liberally provided by

Philipp Becker 6007 7
Geoff Barlow 543 4
DNN-Connect 431 6
Peter Donker 5052 30
Christopher Hammond 679 2
Olivier Jooris 418 1
Daniel Mettler 12001 88
Clint Patterson 1 1
Jos Richters 65 1
James Rosewell 325 2
Will Strohl 1546 27
Ernst Peter Tamminga 436 4
Barry Waluszko 2776 2
Declan Ward 439 1
Gifford Watkins 722 9
Torsten Weggen 2667 3