Table of Contents

Activate 2sxc JavaScript APIs on a Page

you are here (click to zoom) - discover the stack

If you are logged in as SiteAdmin or SystemAdmin, all the JS APIs are already loaded. So if you only want to customize the edit experience for admins, you can skip this step.

If you need to use the JS APIs on public pages, you must first activate them.

Activate the JS APIs in v13+

In 2sxc v14 or newer you would do this:

@inherits Custom.Hybrid.Razor14
@{
  // Tell the page that we need the 2sxc Js APIs
  Kit.Page.Activate("2sxc.JsCore"); 
}

This will load and activate the standard JavaScript APIs and let you get data using the Data Service and the Query Service.

If your public / anonymous users also need CMS features (such as open the edit dialog), you would replace the "2sxc.JsCore" with "2sxc.JsCms". To even create visible toolbars (which requires some CSS) use "2sxc.Toolbars". Read more about activating page features here.

Activate JS APIs in v9.30+

Since v9.30, you can activate the JS APIs using the Razor @Edit.Enable(...) function.

Warning

Avoid this if possible, as we plan to phase it out in the next few years.

<div>
    @Edit.Enable(js: true)
</div>

You can also enable more features, like these:

<div>
    @Edit.Enable(api: true, forms: true, context: true, autoToolbar: false)
</div>

Activate the JS APIs in Older Versions

Before 2sxc 9.30 there was no real API to do this. You had to use a <script> tag like this:

Warning

Avoid this if possible, as we plan to phase it out in the next few years.


How Things Work Internally

Each template that needs the $2sxc-file when not logged in must include it, to be sure it's always there when needed. Note that we've included various features to prevent duplicate execution.

  1. if the file is included multiple times, it will only execute once
  2. if the file is included minified and unminified, it too will only be executed once
  3. if you need to debug the JS for whatever reason with F12 in the browser a sourcemap is included

Note that the only global variable on window created is the $2sxc.


History

  1. Added the script method ca. v4
  2. Added the Edit.Enable v9.30
  3. Replaced Edit.Enable(js:true) with the new IPageService.Activate("2sxc.JsCore") in v13.0