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.
- New JS API .data and .query
- New REST features to set Published state of data
- New REST features to set Metadata targets on create
- 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:
null
or not set - just leave as is / default
true
- set to published
false
- set to not-published
"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