Skip to main content
Home  ›  Blog

Using 2sxc in WebForms, Custom WebAPI or other Razor-Hosts (300)

There are cases where people want to use 2sxc-features inside other wrappers - for example in a custom DNN module using WebForms or in an another pre-existing dnn-module which allows Razor-templating. 

Until now this was very difficult and hacky because you had to rely on internal APIs staying stable, which of course isn't guaranteed. This is now officially supported in v8.3. Note that if you just want to work with the App-Data you should read this post. We stabilized the external API necessary for this, here's how it works:

The ISxcInstance Interface

This is your most important interface as it will contain a full runtime of a 2sxc, ready to consume it's data, review template settings or get data from the App. These are its properties

  1. App - this is a reference to the App-Object, the same as when using it in a razor-template. Contains Settings, Resources, Path, etc.
  2. Data - this is the data all prepared for use in this module. It contains either the items a user added manually, or it contains the data delivered by a query / visual-pipeline.
  3. Template - information about the template about to be used if it would be rendered
  4. Render() - this will return HTML from the correct rendering engine (token or razor)

Internally there is much more going on, but those internals should not be regarded as a public API. Only things we define in interfaces may be regarded as stable.

How do I get this magical Object? 

Because we know that over time, DNN will change, we are providing creation methods specifically for DNN7+; on day we'll probably add other ones for DNN Core. As of now, we provide two official methods to get this:

  1. Factory.SxcInstanceForModule(int modId, int tabId)
  2. Factory.SxcInstanceForModule(ModuleInfo moduleInfo)

Both are located in the namespace ToSic.SexyContent.Environment.Dnn7, so you can either

@using ToSic.SexyContent.Environment.Dnn7

var sxci = Factory.SxcInstanceForModule(420, 56);

Or you can do it like this

var sxci = ToSic.SexyContent.Environment.Dnn7.Factory.SxcInstanceForModule(420, 56);

Using Commands like AsDynamic(…) or AsAdam(…)

If you want to work with the data directly, use them in loops, etc. you'll appreciate the helper-commands which always make life easy. These are bundled in a coding-helper, which you can get with the command

  • CodingHelpers(ISxcInstance sxc)

So your code would look a bit like this: 

@using ToSic.SexyContent.Environment.Dnn7

var sxci = SxcInstanceForModule(420, 56);

var dyn = Factory.CodingHelpers(sxci);

So What's missing?

As of now, this is great for output and using 2sxc-data elsewhere - probably the most common scenario will be when you want to create your custom WebAPI endpoints which are not in an App itself. As of now, there is no support for in-page editing the way 2sxc does it. As of now, I can't think of a good scenario - maybe you have one, then put it in the discussion below.

Some Examples to try

This is Razor-Code which just works in the razor host. Remember to use your module-id and your tab-id.

Render

@using ToSic.SexyContent

@using ToSic.SexyContent.Environment.Dnn7

@{

    var moduleId = 420;

    var tabId = 56;

    var sxci = Factory.SxcInstanceForModule(moduleId, tabId);

}

@sxci.Render()

Working with Data

@using ToSic.SexyContent

@using ToSic.SexyContent.Environment.Dnn7

@{

    var moduleId = 420;

    var tabId = 56;

    var sxci = Factory.SxcInstanceForModule(moduleId, tabId);

}

@sxci.Render()

Enjoy...

Love from Switzerland, 
Daniel & the 2sic team


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