Accessing 2sxc Data from outside of 2sxc

Feb 12, 2016 at 3:28 PM
Hi,

What's the best way of getting data out of 2sxc within something like a standard Razor view. I have another DNN module in which I need to display data from 2sxc. Preferably without having to do anything client side for SEO reasons.

Thanks
Coordinator
Feb 23, 2016 at 6:18 PM
this is a scenario we just didn't spend time on, as 2sxc hosts razor I don't know a good reason why you would want to do the same thing without 2sxc.

it's like having a starbucks coffee inside a nescafe :)
Feb 24, 2016 at 9:06 AM
I worked it out...

In this situation I needed to combine data from 2sxc and another module to display it within one Razor view which is hosted by the 'other' module... Pretty simple in the end...

Added this to the other Razor View:
private static AppAndDataHelpers _appAndDataHelpers;
        private static AppAndDataHelpers AppAndDataHelpers
        {
            get
            {
                if (_appAndDataHelpers == null)
                {
                    var moduleInfo = DotNetNuke.Entities.Modules.ModuleController.Instance.GetModule(ModuleIdOf2xcModule, TabIdOf2xcModule, false);
                    int? appId = null;
                    if (!appId.HasValue)
                        appId = SexyContent.GetAppIdFromModule(moduleInfo);
                    var zoneId = SexyContent.GetZoneID(moduleInfo.PortalID);
                    SexyContent Sexy = new SexyContent(zoneId.Value, appId.Value, true, moduleInfo.OwnerPortalID);
                    var viewDataSource = Sexy.GetViewDataSource(moduleInfo.ModuleID, SexyContent.HasEditPermission(moduleInfo), Sexy.ContentGroups.GetContentGroupForModule(moduleInfo.ModuleID).Template);
                    _appAndDataHelpers = new AppAndDataHelpers(Sexy, moduleInfo, (ToSic.SexyContent.DataSources.ViewDataSource)viewDataSource, Sexy.App);
                }
                return _appAndDataHelpers;
            }
        }
Coordinator
Feb 24, 2016 at 10:34 AM
Cool that you figured it out so quickly :)

i believe that there are many users trying to do something similar - care to blog about it?

Maybe also with some tipps that you can then use this helper and call things like AsDynamic etc.?
Feb 24, 2016 at 10:58 AM
Cheers...

I've got deadlines looming with 2 huge projects at the moment... but sure when I get a bit of time I will, but I'll have to sort my blog out too!
Coordinator
Feb 28, 2016 at 4:45 AM
Hey Jay

Just a heads up - you're using a lot of internal API there which had just grown over many years. As you probably noticed, the SexyContent object had lots of code which wasn't easy to figure out. As I see that people are starting to use this externally, I had to do a clean up. This means that various commands you just used here are now moved away to a more stable location.

This means that this will be a breaking change for you. It will be easy to update your work as it still does the same stuff, but you'll need to rename some calls here.
Coordinator
Feb 28, 2016 at 5:41 AM
Edited Feb 28, 2016 at 5:42 AM
@JaxUk - I'm back :). I've created an "EngineContext" object which should reduce my API surface and give you everything you need. to use it, you would call something like
  var eContext = new ToSic.SexyContent.Environment.Dnn7.EngineContext(tabId, modId);
  var helpers = eContext.Helpers();
This will still allow me enough freedom to move around the internal api as it grows, and you should have everything you need. Here's my (interim) code:
using DotNetNuke.Entities.Modules;
using ToSic.SexyContent.DataSources;
using ToSic.SexyContent.Statics;

namespace ToSic.SexyContent.Environment.Dnn7
{
    /// <summary>
    /// This object is for other engines like razor or WebForms wanting to work with 2sxc data. 
    /// Just initialize the object with tab-id and module id, 
    /// you then get
    /// - SxcInstance - the core instance object containing App, Template, etc.
    /// - App
    /// </summary>
    public class EngineContext
    {
        public int TabId { get; }
        public int ModuleId { get; }

        public EngineContext(int tabId, int modId)
        {
            TabId = tabId;
            ModuleId = modId;
        }

        private ModuleInfo _moduleInfo;
        public ModuleInfo ModuleInfo => _moduleInfo ??
                                        (_moduleInfo = ModuleController.Instance.GetModule(ModuleId,
                                            TabId, false));

        private SexyContent _sxcInstance;

        public SexyContent SxcInstance
        {
            get
            {
                if(_sxcInstance!= null)
                    return _sxcInstance;
                int? appId;
                appId = AppHelpers.GetAppIdFromModule(ModuleInfo);
                var zoneId = ZoneHelpers.GetZoneID(ModuleInfo.PortalID);
                _sxcInstance = new SexyContent(zoneId.Value, appId.Value, true, ModuleInfo.OwnerPortalID);
                    return _sxcInstance;
            }
        }

        private AppAndDataHelpers _appAndDataHelpers;
        public AppAndDataHelpers Helpers()
        {
            if (_appAndDataHelpers != null)
                return _appAndDataHelpers;

            var viewDataSource = ViewDataSource.ForModule(ModuleInfo.ModuleID, SecurityHelpers.HasEditPermission(ModuleInfo), SxcInstance.ContentGroups.GetContentGroupForModule(ModuleInfo.ModuleID).Template, SxcInstance);
            _appAndDataHelpers = new AppAndDataHelpers(SxcInstance, ModuleInfo, viewDataSource, SxcInstance.App);

            return _appAndDataHelpers;
        }
    }
}
Coordinator
Mar 2, 2016 at 10:10 AM
Drumroll!!!!

It's ready - pls check out 2sxc 8.3 :)

I wrote a short blog about it here: http://2sxc.org/en/blog/post/using-2sxc-in-webforms-custom-webapi-or-other-razor-hosts-300

Would love to get your feedback on this
Mar 11, 2016 at 2:23 PM
I'm going to have a play with this next week, I'll let you know how I go!
Jun 21, 2016 at 10:46 AM
Hey,

Sorry for taking so long to get back to you... I've just upgraded 2sxc on the client site needing this and updated the code...

Much easier to get to the data now.

Cheers