How to access Another Module's Content and Presentation Items after 2sxc v10.20+

162 Views Asked by At

Here is code that worked up through 2sxc 10.9.1. Though I am able to get the CmsBlock for the TabID, ModuleID and get that to .Render(), I need more. Here is the old code. Not sure it makes any difference, but this View is using the normal Link content-type and is running in an older version of the Content App (appx 3.03=ish). 2sxc has been upgraded and is now 11.22.0 LTS.

I have removed unnecessary stuff, so I doubt this runs as is...

@using ToSic.Razor.Blade
@using ToSic.SexyContent.Environment.Dnn7

@{
  var Helpers = CreateInstance("_Helpers.cshtml");

  // Display the items from the Manage Links module, we go in 'sideways'
  // this gives us just the Content items with their Presentations settings, etc.
  var sxci = Factory.SxcInstanceForModule(3360, 606); // ModuleID of Manage Links
  var dyn = Factory.CodingHelpers(sxci);
  var allLinks = dyn.AsDynamic(dyn.Data["Default"]); 
}

@* other stuff *@

<div class="row co-documents justify-content-center align-items-center">
  @foreach (var linkItem in allLinks) {
    var linkInfo = Helpers.LinkInfos(linkItem.Link, linkItem.Window, linkItem.Icon);
    string iconStyle = linkItem.IconStyle ?? "fas";
    int linkColumns = (int)linkItem.Presentation.Columns;
    string linkIconAlign = linkItem.Presentation.IconAlign;
    string linkIconBGColor = linkItem.Presentation.IconBGColor;

@* other stuff *@

  }
</div>

So the easy thing to figure out was how to get the module as a CmsBlock which I can Render() as is (below), but what I need to do instead is get proper access to the List of Content Items AND their Presentation data (like above, allLinks).

ToSic.Sxc.Dnn.Factory.CmsBlock(606, 3360).Render();

What am I missing? How can I get access to the other module's data like I was doing before? In this case, I do this in 3 different places on the website. So to outline this in English, I have a module that the client manages a few special links that get displayed in MegaMenus, other special nav, and directly on a couple of pages. In each place they render differently. In their "home" module, where they get edited, they just look boring like this:

enter image description here

I realize its something like this:

var allLinks = something1.AsList(something2.Data["Default"]);

I understand that something2 is an app instance, but how do I create it in the context of the other module?

And what is something1 nowadays? And how do instantiate it? Looks like its a new ToSic.Sxc.Code.DynamicCode() but I can't figure out how to construct that in a way that I can use or doesn't just throw errors.

Thanks in advance for any insight!!

2

There are 2 best solutions below

2
On BEST ANSWER

Okay, it took a little testing, trial and error. And also I missed that DynamicCode() was a Method of the Factory class. In retrospect it does seem easy now.

So first you get the BlockBuilder

  var block = Factory.CmsBlock(606, 3360);

Then you get the DynamicCode instance (Code.DnnDynamicCodeRoot) from that

var dc = Factory.DynamicCode(block);

And then things are normal

var allLinks = AsList(dc.Data["Default"]);

The rest of the code works like it did before; I can foreach through the links with Header (renamed from ListContent) and Presentation (now Content.Presentation) working just as expected.

0
On

The above answer works fine if you are inside the C# Razor template of the 2sxc View. But what if you are outside, for example in a Razor template for a DDR Menu?

Same two steps as above (get the block and the dc), but then you do NOT have access to AsList() or the App. Thankfully, you already have DynamicCode, so you could just get all the records in the Bibliography content-type like this:

<ul>
var items = dc.AsList(dc.App.Data["Bibliography"]);
foreach (var item in items) 
{
  <li>@item.EntityTitle</li>
}
</ul>

So once you've got your dc you've got access to all the usual 2sxc toys.