Skip to content

Instantly share code, notes, and snippets.

@jeremy-farrance
Last active November 11, 2019 20:49
Show Gist options
  • Save jeremy-farrance/cb1bde1834e3223e233c5659c60a1d40 to your computer and use it in GitHub Desktop.
Save jeremy-farrance/cb1bde1834e3223e233c5659c60a1d40 to your computer and use it in GitHub Desktop.
DNN RazorHost or DDRMenu RazorScript to access 2sxc data (Content-Types) using AsDynamic()
// from PDEG project 2019
@using ToSic.SexyContent.Environment.Dnn7
var sxci = Factory.SxcInstanceForModule(3354, 606); // ModID of Flyout Menu editor, TabID of SITE/Manage Flyouts Links
var dyn = Factory.CodingHelpers(sxci);
var appContent = Factory.App(2); // Content App is usually 2, but not always
var myPageDetails = dyn.AsDynamic(appContent.Data["Content"]);
<pre>
Count = @myPageDetails.Count()
First = @myPageDetails.First().EntityTitle;
// above outputs:
// Count = 303
// First = Demo Page, default;
</pre>
@jeremy-farrance
Copy link
Author

jeremy-farrance commented Nov 9, 2019

Note that if you have any 2sxc Content App on a page, just replace that ModuleID and TabID above and you will get output. The Count will be the number of records in your Content-Type: Content (Basic) - 2sxc v10.09.x

@jeremy-farrance
Copy link
Author

Here is the related article, but note that (as of Nov 2019) the "Working with Data" example is wrong (just a duplicate of the "Render" example above it).

@jeremy-farrance
Copy link
Author

jeremy-farrance commented Nov 11, 2019

From a .ascx you could do this:

<script runat="server">
private IHtmlString RenderOutput()
  {
    return ToSic.SexyContent.Environment.Dnn7.Factory.SxcInstanceForModule(3354, 606).Render();
  }
</script>
<%=RenderOutput() %>

Note: probably a good opportunity to use object (DNN) caching?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment