Requirements
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.
Formulas V2 has intellisense, stoppability, support for promises and more.
Get Data for a Field from a Web API
Get value for a field from a WebAPI
Try it:
This formula is very advanced, and will initialize a fields value (if empty) from a WebAPI call.
🆕 in v2 all formulas returning a promise are stopped by default!
This is why the code is much simpler compared to v1.
If ever necessary, the promise returned can specify to not stop the formula, but we don't need this for the current demo.
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)
v2((data) => { // Only enable once data has been loaded return !data.Title; });
Field.Value (Formula-Target: Field.Value)
v2((data, context) => { // for demo reasons, don't do this till start is toggled. if (!data.Start) return data.value; // Call the sxc web-api controller from other tutorials return context.sxc.webApi.fetchJson('app/auto/api/basic/hello') .then(data => { console.log('got data', data); // log for demo reasons return data; // Use the result as the new value }); });
Get dropdown options from WebAPI
Try it:
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)
v2((data, context) => { // Call the sxc web-api controller from other tutorials return 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'); return lines; }); });
Get dropdown options from WebAPI accessing App.Data
Try it:
This is an advanced formula which will call a WebApi to get the possible values in a drop down - from Data in the App.
Click on the (Σ) button above to see the edit-UI with the formula.
Formulas of FormulasDropDownWebApiData.Dropdown
Setting DropdownValues (Formula-Target: Field.Settings.DropdownValues)
v2((data, context) => { // Call the sxc web-api controller from other tutorials const promise = context.sxc.webApi.fetchJson('app/auto/api/formulas/Persons') .then(result => { console.log('got result', result);// log for demo reasons // The result is a dictionary, so we convert to lines return Object.keys(result) .map(k => k + ":" + result[k] + ' (' + k + ')') .join('\n'); }); // Return a loading value and a promise for later... return { value: data.value, promise }; });
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; <!-- unimportant stuff, hidden --> <div @Sys.PageParts.InfoWrapper()> @Html.Partial("../shared/DefaultInfoSection.cshtml") <div @Sys.PageParts.InfoIntro()> <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> @Html.Partial("./Part NowUsingV2.cshtml") </div> </div> @* Get Value from WebApi *@ <h2>Get Data for a Field from a Web API</h2> @* Dropdown Values From WebAPI *@ @* Dropdown Values From WebAPI App.Data *@ @* Footer *@ @Html.Partial("../Shared/Layout/FooterWithSource.cshtml", new { Sys = Sys })