Skip to main content
Home  › ... Razor

Data Tutorials

Tutorial HomeData
#6 Query with Params - Run multiple times
Requirements

Run an App Query multiple times on the same page

The previous examples showed how to use a Query. Now we want to run the query twice, which requires a Reset().
Note that the parameter SiteId is preset to be [Site:Id].

Initial Code

The following code runs at the beginning and creates some variables/services used in the following samples.

@{
   // Different query in DNN / Oqtane - eg. "SqlTop10FilesDnn"
   var queryName = "SqlTop10Files" + CmsContext.Platform.Name;

   // get the query and ask for the "Default" results as a dynamic List
   // This case doesn't set parameters, so we're using the defaults (current site)
   var query = Kit.Data.GetQuery(queryName);
   var currentFiles = AsList(query);

   // Get a fresh query, so we can use again with other parameters
   query = Kit.Data.GetQuery(queryName, parameters: new { 
     SiteId = CmsContext.Platform.Name == "Dnn" ? 0 : 1
   });
   var rootFiles = AsList(query);
 }

Files from the current Portal

Output

  • image-1.jpg
  • image-2.jpg
  • ct basic content.png
  • ct image.png
  • ct layout element.png
  • ct links.png
  • ct location.png
  • ct person.png
  • ct video.png
  • RazorTutoApp1000x1000.png
<ul>
 @foreach (var file in currentFiles) {
  <li>@file.Name</li>
}
</ul>

Files from the root portal

Output

  • 2sic120px.gif
  • WYSIWYG.css
  • PlaceHolder.txt
  • PlaceHolder.txt
  • Default.page.template
  • Chrysanthemum.jpg
  • DemoImage.jpg
  • DemoImageBox.jpg
  • DemoImageBox2.jpg
  • DemoImagex-y.jpg
<ul>
 @foreach (var file in currentFiles) {
  <li>@file.Name</li>
}
</ul>
#6 Query with Params - Run multiple times

Source Code of this file

Below you'll see the source code of the file. Note that we're just showing the main part, and hiding some parts of the file which are not relevant for understanding the essentials. Click to expand the code

@inherits Custom.Hybrid.Razor14
@using ToSic.Razor.Blade;
@using System.Linq;
<!-- unimportant stuff, hidden -->


<div @Sys.PageParts.InfoWrapper()>
  @Html.Partial("../shared/DefaultInfoSection.cshtml")
  <div @Sys.PageParts.InfoIntro()>
    <h2>Run an App Query multiple times on the same page</h2>
    <p>
      The previous examples showed how to use a <code>Query</code>. 
      Now we want to run the query twice, which requires a <code>Reset()</code>. <br/>
      Note that the parameter <code>SiteId</code> is preset to be <code>[Site:Id]</code>. 
    </p>

  <div class="row">
    <div class="col-4">@Sys.Fancybox.PreviewWithLightbox(App.Path + "/data/assets/sql-query-dnn-files.png", 200, 200, "float-left", label: "Query Tree") </div>
    <div class="col-4">@Sys.Fancybox.PreviewWithLightbox(App.Path + "/data/assets/sql-query-configuration.png", 200, 200, "float-left", label: "Query Configuration with Params and Test-Values")</div>
    <div class="col-4">@Sys.Fancybox.PreviewWithLightbox(App.Path + "/data/assets/sql-query-select-statement.png", 200, 200, "float-left", label: "SQL Query using Params")</div>
  </div>

  </div>
</div>

 @{
    // Different query in DNN / Oqtane - eg. "SqlTop10FilesDnn"
    var queryName = "SqlTop10Files" + CmsContext.Platform.Name;

    // get the query and ask for the "Default" results as a dynamic List
    // This case doesn't set parameters, so we're using the defaults (current site)
    var query = Kit.Data.GetQuery(queryName);
    var currentFiles = AsList(query);

    // Get a fresh query, so we can use again with other parameters
    query = Kit.Data.GetQuery(queryName, parameters: new { 
      SiteId = CmsContext.Platform.Name == "Dnn" ? 0 : 1
    });
    var rootFiles = AsList(query);
  }



<h3>Files from the current Portal</h3>

  <ul>
   @foreach (var file in currentFiles) {
    <li>@file.Name</li>
  }
  </ul>


  </div>
</div>

<h3>Files from the root portal</h2>

  <ul>
   @foreach (var file in rootFiles) {
    <li>@file.Name</li>
  }
  </ul>



@* Footer *@
@Html.Partial("../Shared/Layout/FooterWithSource.cshtml", new { Sys = Sys })