Skip to main content
Home  › ... 2sxc Apps

Data Tutorials

Tutorial Home
#4 Use Query to get CSV Data from a File Configured in Settings

Example from CSV which was uploaded as an App Setting

This is similar to the previous example, except that the CSV file can be uploaded again and again in the App-Settings UI. So the path isn't hard-coded.


@{
  // get the query
  var query = App.Query["ProductsFromCsvInAppSettings"];

  // get the data from the query
  var products = AsList(query);

  // get the current author (if available) from the stream Current
  var current = AsList(query["Current"]).FirstOrDefault();
}
<ul>
  @foreach(var product in products) {
    <!-- this li will have class=selected if it's the current one -->
    <li class='@(product == current ? "selected" : "")'>
      <!-- this creates a link to the current tutorial (data220) and product=id -->
      <a href='@Link.To(parameters: "data231=true&product=" + product.EntityId)'>
        @product.Name (#@product.Id)
      </a>
      @if(current == product) {
        <br>
        <em>
          @current.Description 
          (<a href="@product.Link" target="_blank">find out more</a>)
        </em>
      }
    </li>
  }
</ul>

Error showing file source

Where was a problem showing the file source for /Portals/0/2sxc/Tutorial-Razor/data/assets/products.csv

Source Code of assets/products.csv

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


  
#4 Use Query to get CSV Data from a File Configured in Settings

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
<!-- unimportant stuff, hidden -->
@using ToSic.Razor.Blade;
@using System.Linq;
Example from CSV which was uploaded as... <!-- unimportant stuff, hidden -->

@{
  // get the query
  var query = App.Query["ProductsFromCsvInAppSettings"];

  // get the data from the query
  var products = AsList(query);

  // get the current author (if available) from the stream Current
  var current = AsList(query["Current"]).FirstOrDefault();
}
<ul>
  @foreach(var product in products) {
    <!-- this li will have class=selected if it's the current one -->
    <li class='@(product == current ? "selected" : "")'>
      <!-- this creates a link to the current tutorial (data220) and product=id -->
      <a href='@Link.To(parameters: "data231=true&product=" + product.EntityId)'>
        @product.Name (#@product.Id)
      </a>
      @if(current == product) {
        <br>
        <em>
          @current.Description 
          (<a href="@product.Link" target="_blank">find out more</a>)
        </em>
      }
    </li>
  }
</ul>




<!-- unimportant stuff, hidden -->

<!-- unimportant stuff, hidden -->