Error Showing Content - please login as admin for details.
Error Showing Content - please login as admin for details.
#7 Formulas using REST or WebApi Calls
Formulas using WebAPI and REST calls
Formulas can retrieve data from WebAPIs and REST.
We have various examples of this on other pages, so this page will just repeat the examples from those page.
Get Data for a Field from a Web API
Get value for a field from a WebAPI
This formula is very advanced, and will initialize a fields value (if empty) from a WebAPI call.
Click on the (Σ) button above to see the edit-UI with the formula.
Formulas of FormulasWebApiText.Title
Field.Settings.Disabled (Formula-Target: Field.Settings.Disabled)
function v1 (data, context) {
// Only enable once data has been loaded
return !data.Title;
}
Field.Value (Formula-Target: Field.Value)
function v1 (data, context) {
// for demo reasons, don't do this till start is toggled.
if (!data.Start) return data.value;
// If it has a value, keep it, don't look for another
if (data.value) return data.value;
// If data had been stored in the cache by the api-call, return it
if (context.cache.data) return context.cache.data;
// Prevent the API call from running twice
if (context.cache.alreadyRun) return data.value;
context.cache.alreadyRun = true;
// Call the sxc web-api controller from other tutorials
context.sxc.webApi.fetchJson('app/auto/api/basic/hello')
.then(data => {
console.log('got data', data);// log for demo reasons
context.cache.data = data; // set cache for next formula cycle
context.form.runFormulas(); // trigger the next formula cycle
});
// After starting the web-api call, return the current empty value
return data.value;
}
Get DropDown options from a WebAPI
Get dropdown options from WebAPI
This is an advanced formula which will call a WebApi to get the possible values in a drop down.
Click on the (Σ) button above to see the edit-UI with the formula.
Formulas of FormulasDropdownWebApi.Dropdown
Field.Settings.DropdownValues (Formula-Target: Field.Settings.DropdownValues)
function v1 (data, context) {
// If it has a value, keep it, don't look for another
if (data.value) return data.value;
// If data had been stored in the cache by the api-call, return it
if (context.cache.data) return context.cache.data;
// Prevent the API call from running twice
if (context.cache.alreadyRun) return data.value;
context.cache.alreadyRun = true;
// Call the sxc web-api controller from other tutorials
context.sxc.webApi.fetchJson('app/auto/api/formulas/OptionsFromBackend')
.then(data => {
console.log('got data', data);// log for demo reasons
// The result is a dictionary, so we convert to lines
const lines = Object.keys(data)
.map(k => k + ":" + data[k])
.join('\n');
context.cache.data = lines; // set cache for next formula cycle
context.form.runFormulas(); // trigger the next formula cycle
});
}
// Add namespaces for security check in Oqtane & DNN despite differences in .net core/.net Framework
// If you only target one platform, you can remove the parts you don't need
#if NETCOREAPP
using Microsoft.AspNetCore.Authorization; // .net core [AllowAnonymous] & [Authorize]
using Microsoft.AspNetCore.Mvc; // .net core [HttpGet] / [HttpPost] etc.
#else
using System.Web.Http; // .net 4.5 [AllowAnonymous] / [HttpGet]
using DotNetNuke.Web.Api; // [DnnModuleAuthorize] & [ValidateAntiForgeryToken]
#endif
using System.Collections.Generic; // To use the Dictionary
[AllowAnonymous] // all commands can be accessed without a login
public class FormulasController : Custom.Hybrid.Api14 // see https://r.2sxc.org/CustomWebApi
{
[HttpGet] // [HttpGet] says we're listening to GET requests
public object OptionsFromBackend()
{
var results = new Dictionary<string, string>() {
{ "first", "First option from WebApi" },
{ "second", "Second option from WebApi"},
{ "third", "3rd option" }
};
return results;
}
}
// The next line is for 2sxc-internal quality checks, you can ignore this
// 2sxclint:disable:no-dnn-namespaces - 2sxclint:disable:no-web-namespace
#7 Formulas using REST or WebApi Calls
@inherits Custom.Hybrid.Razor14
@using ToSic.Razor.Blade;
<!-- unimportant stuff, hidden -->
<div class="row">
<div class="col-md-7">
<h2>Formulas using WebAPI and REST calls</h2>
<p>
Formulas can retrieve data from WebAPIs and REST.
We have various examples of this on other pages, so this page will just repeat the examples from those page.
</p>
</div>
@Html.Partial("../shared/_DefaultInfoSection.cshtml")
</div>
<h2>Get Data for a Field from a Web API</h2>
@* Get Value from WebApi *@
@Html.Partial("_PartFormulas.cshtml", sharedExamples.ValueFromWebApi())
<hr>
<h2>Get DropDown options from a WebAPI</h2>
@* Dropdown Values From WebAPI *@
@Html.Partial("_PartFormulas.cshtml", sharedExamples.DropdownFromWebApi())
@Html.Partial("../shared/_source-code.cshtml", new {
Path = App.PhysicalPath + "/api/",
File = "FormulasController.cs",
Size = 600,
TitlePath = "/api/"
})
<!-- unimportant stuff, hidden -->