Skip to main content
Home  ›  Blog

New JS and REST APIs in 2sxc 13

New JS and REST APIs let you easily read, create and edit 2sxc data in JavaScript or using mobile apps. 

Overview

2sxc 13 introduces a plethora of functionality to make reading and writing data easier from other systems. 

  1. New JS API .data and .query
  2. New REST features to set Published state of data
  3. New REST features to set Metadata targets on create
  4. New REST features to set relationships on create

New JavaScript APIs

The previous JS APIs were available on sxc.webApi and historically used jQuery promises. We believe that jQuery is obsolete, so we wanted to create simple, new APIs which matches the modern way we use JavaScript and uses fetch

Here's an example of code requesting data in the new API:

<script>
const
modId = @CmsContext.Module.Id;
const sxc = $2sxc(modId);
const authorsSvc = sxc.data('Authors');
authorsSvc.getAll().then(authors => console.log(authors));
</script>

The new APIs have the following commands

  • sxc.data("ContentTypeName") will get you a object to work with data of this type. All the commands return standard promises. You can read more about it in the docs. These are the commands on that object: 
    • getAll()
    • getOne(id)
    • create(objectWithValues)
    • delete(id) and delete(guid)
    • update(id, objectWithValues)
  • sxc.query("QueryName") will get you an object to work with this query. All the commands return standard promises. You can read more about it in the docs. These are the commands on that object:
    • getAll()
    • getAll(urlParams) - other signatures also available
    • getStream(streamName) - other signatures also available
    • getStreams(streamNames) - other signatures also available

This should help you create awesome JS based applications using 2sxc data.

👉 also check out the tutorials

REST now controls Publishing and Draft

Previously data created using REST and JavaScript was always published or draft, based on the user permissions. Now - assuming permissions allow - your REST request can provide as IsPublished to determine if something should be published or not. Allowed values are:

  1. null or not set - just leave as is / default
  2. true - set to published
  3. false - set to not-published
  4. "draft" - set the change to draft, leave the previous published version published

This is also documented in more details in the docs.

REST can can Add-To-Parent

When creating a new item you can also set a property ParentRelationship. It must be an object with these fields:

{
"Parent": "guid-guid-guidguid-guid-guid-guid",
"Field": "Tags",
"Index": 0
}

Then upon creation it will add a reference to this new item to the parent, in that field on the specified index. Your more about it in the docs

REST can create Metadata

When creating an item, you can say that it it metadata, and assign it to a target which it will be describing. This uses the For property. You can read more about it in the docs.

TL;DR

Have fun pimping your JS Apps!
Love from Switzerland, 
iJungleboy

 


Daniel Mettler grew up in the jungles of Indonesia and is founder and CEO of 2sic internet solutions in Switzerland and Liechtenstein, an 20-head web specialist with over 800 DNN projects since 1999. He is also chief architect of 2sxc (see github), an open source module for creating attractive content and DNN Apps.

Read more posts by Daniel Mettler