#5 Create Metadata using JS APIs
Requirements
Use the sxc data API to create metadata
This page uses the sxc data API to create metadata in the backend.
In this tutorial you'll learn how to:
-
Create metadata for other entities using the javascript
.create(..., ...)
method -
Delete existing items (so the demo doesn't grow uncontrollably) using the
.delete(...)
method
Look at the content below to see the effect.
Name | Memberships | |
---|---|---|
Gale Hansen | 225969 delete | |
Gerard Pitts | 7892345 delete | |
Todd Anderson | ||
Richard Cameron | 7922348 delete | |
Daniel |
#5 Create Metadata using JS APIs
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; @{ // Tell the page that we need the 2sxc Js APIs Kit.Page.Activate("2sxc.JsCore"); } <!-- unimportant stuff, hidden --> <div @Sys.PageParts.InfoWrapper()> @Html.Partial("../shared/DefaultInfoSection.cshtml") <div @Sys.PageParts.InfoIntro()> <h2>Use the sxc data API to create metadata</h2> <p>This page uses the sxc data API to create metadata in the backend.</p> <br> In this tutorial you'll learn how to: <ul> <li> Create metadata for other entities using the javascript <code>.create(..., ...)</code> method </li> <li> Delete existing items (so the demo doesn't grow uncontrollably) using the <code>.delete(...)</code> method </li> </ul> <br> Look at the content below to see the effect. </div> </div> <table id="example-content" class="table"> <thead> <tr> <th>Name</th> <th>Memberships</th> <th></th> </tr> </thead> <tbody> @foreach (var poet in AsList(App.Data["Poets"])) { var membershipMd = AsList(poet.Metadata.OfType("DeadPoetSocietyMembership") as object); <tr> <td>@poet.Name</td> <td> @if (membershipMd.Any()) { foreach (var membership in membershipMd) { <span> @membership.MembershipNumber <a href="#" onclick="window.sxcDataTutorial240.del(@membership.EntityId)">delete</a> </span> } } else { <button type="button" class="btn btn-primary" onclick='window.sxcDataTutorial240.add("@poet.EntityGuid")'> add membership (metadata) </button> } </td> </tr> } </tbody> </table> @* This tutorial uses turnOn, see turnOn tutorials *@ @{ var data = new { moduleId = CmsContext.Module.Id }; } @Kit.Page.TurnOn("window.sxcDataTutorial240.init()", data: data) <script src="@CmsContext.View.Path/310-create-metadata.js" @Kit.Page.AssetAttributes()></script> @* Footer *@ @Html.Partial("../Shared/Layout/FooterWithSource.cshtml", new { Sys = Sys })