How would you call an Agility Page Module from another Controller?

Using Agility's API how can I get the data of a module on a particular page directly outside of it's controller action defined within Agility?

Is there a function in the API similar to AgilityModule.GetModule(pagepath, id)? or something similar?



1 comment
  • Official comment

    Hi Giovanni,

    If you find yourself needing to do this, I'd recommend adjusting the architecture to utilize Shared Content so that the 'meat and the potatoes' of the data you need is in Shared Content and can easily be accessible outside the context of a page and the module's code. This will be much easier and maintainable.

    If that isn't an option, then you can do this:

    var moduleContent = new AgilityContentRepository<Module_Jumbotron("home_Jumbotron").Item("");

    Where "Module_Jumbotron" is the strongly typed class of the module and "home_Jumbotron" is the Reference Name of the module.

    The problem with the above approach is that you are never going to really know what the Reference Name for the module on the page is going to be as it is auto-generated. You can check after a module has been added to a page by clicking on the "Settings" tab. If you need to dynamically get the reference name, then you'll need to do some additional work to look up the page by path and then iterate through the modules to get the content reference name based on the matching Module Definition you are looking for.

    var moduleReferenceName = Agility.Web.Data.GetPage("~/home").ContentSections.FirstOrDefault(i => i.ModuleID == 10).ContentReferenceName; //need to know what your Module Def ID is, you can find this by inspecting the data in ContentSections in your debugger...
    var moduleContent = new AgilityContentRepository<Module_Jumbotron>(moduleReferenceName).Item("");

    So, not so elegant and could cause issues for you down the line if someone changes the page name, or removes the module from the page.

    Hope that helps!

    Comment actions Permalink

Please sign in to leave a comment.

Didn't find what you were looking for?

New post