Skip to main content
Home  ›  Blog

Reusable Queries with Params in 2sxc 10.23

2sxc 10.23 introduces Params, a massive enhancements to queries - allowing better overview, better code-use and more 🤜...

How to Use 1) In Visual Query

Basically you can now create a visual query as previously. But as part of the Query Settings you can now also specify a list of parameters and provide defaults. These parameters can also default to another token, so you can write things like one of the following:

  1. Sort=Asc
  2. Sort=[QueryString:Sort]
  3. Sort=[QueryString:Sort||Asc]

In your query parts you can now use the token [Params:Sort] where you need it. From now on we recommend to place all parameters in the Params, because it makes it clearer for people working with the query what factors will affect the output. 

Here's what the query looks like it this example:

And here's the configuration of the Sql Query:

You can of course still test your queries, and also provide alternate Test-values in the Test Parameters.

How to Use 2) in Code

Once you have created a query, you can now also run the query in code with parameters set from the code - like this:

Demo using Query Params in Code

var query = App.Query["GetPosts"];
query.Params("Sort", "desc");
@foreach(var post in AsList(query)) {
 @post.Title </br>
}

What's it For?

In 2sxc it's super easy to create visual queries that filter/sort/page/cache data. These queries were originally meant for direct connection to Razor templates or SPA REST APIs. 

Previously these queries could use parameters like [QueryString:id], [App:Setting:DefaultSort] or combinations thereof like [QueryString:Sort||[App:Settings:DefaultSort]]. This was great for many use cases, but there were 3 important use cases not covered:

  1. If you wanted to use a query in a C# Razor Template but change the parameters, you kind of had to first make sure the parameter was in the URL or in another LookUp source. This was not cool.
  2. If you wanted to modify certain query behavior like inject a parameter based on the user property (IsEditor...) this was almost impossible, resulting is some creative workarounds.
  3. If you wanted to use the query in a custom WebApi controller just for looking up something (which needed a parameter), this was really difficult. 
  4. To run the query multiple times in your same code wasn't super-hard. For example to run it with the parameter "Category=Web" and also "Category=IT"
  5. If you wanted to re-use a query inside another query, that wasn't possible. Very soon we'll release a VisualQuery Component that makes this possible :)

Hope you love it

Get 2sxc 10.23 from Github, which also has lots of additional enhancements. Also check out the App.Query tutorials in the DNN-Razor Tutorials.

Love from Switzerland,
iJungleboy


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 800 DNN projects since 1999. He is also chief architect of 2sxc (see github), an open source module for creating attractive content and DNN Apps.

Read more posts by Daniel Mettler