Skip to main content

JavaScript API Tutorials

Tutorial Home

Use the sxc query(...) API to get Queries in JS

This page uses the sxc data API to get data from the backend and display it in a table with JavaScript.
In this tutorial you'll learn how to:

  • Create a $2sxc object using the current Module Id
  • Create a Query service using the .query(...).
  • Read data using getAll(), getStream(...) and getStreams(...)

Note that this sample will run the query RandomAuthorWithBooks. There are some things which you should know:

  1. The Query has permissions configured to allow viewers to use the query in JS.
  2. Each call returns a random author with his books, so sometimes the books-list may be empty

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;
@{
  // Tell the page that we need the 2sxc Js APIs
  Kit.Page.Activate("2sxc.JsCore"); 
}
<!-- unimportant stuff, hidden -->
Use the sxc query(...) API to get... <!-- unimportant stuff, hidden -->

<button id="mod-@CmsContext.Module.Id-load-all" type="button" class="btn btn-primary">Get entire Query</button>
<button id="mod-@CmsContext.Module.Id-author" type="button" class="btn btn-primary">Get Stream <code>Author</code></button>
<button id="mod-@CmsContext.Module.Id-streams" type="button" class="btn btn-primary">Get Streams <code>Author</code> and <code>Books</code></button>

@* This tutorial uses turnOn, see https://app-dev.2sxc.org/tutorial-razor/en-bs4/Home/turn-on/home *@
@{ Kit.Page.Activate("turnOn"); }
<turnOn turn-on='{ "run": "window.tutQuery.init()", "data": { "moduleId": "@CmsContext.Module.Id" } }'></turnOn>

<script src="@App.Path/js/_220-query.js"></script>

<!-- unimportant stuff, hidden -->

Source Code of _220-query.js

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

window.tutQuery = {
  querySvc: null,
  init: function({ moduleId }) {
    // Create a $2sxc object using the current Module Id
    const sxc = $2sxc(moduleId);
    this.querySvc = sxc.query('RandomAuthorWithBooks');

    // Attach click-handlers to button
    document.getElementById(`mod-${moduleId}-load-all`).onclick = () => this.getAll();
    document.getElementById(`mod-${moduleId}-author`).onclick = () => this.getAuthor();
    document.getElementById(`mod-${moduleId}-streams`).onclick = () => this.getStreams();
  },

  getAll: function() {
    this.querySvc.getAll().then(data => {
      console.log("Get All", data);
      alert('Got all - see console for details. \n \n' + JSON.stringify(data, null, 2));
    });
  },

  getAuthor: function() {
    this.querySvc.getStream('Author').then(data => {
      console.log("Get Stream 'Author' only", data);
      alert('Got all - see console for details. \n \n' + JSON.stringify(data, null, 2));
    });
  },

  getStreams: function() {
    this.querySvc.getStreams('Author,Books').then(data => {
      console.log("Get Streams 'Author' and 'Books'", data);
      alert('Got all - see console for details. \n \n' + JSON.stringify(data, null, 2));
    });
  },
}