Page plugin development

Page plugin is an add-on to Kooboo CMS, and is responsible for making data source available for page access. It is similar to module, but while module contains user interface for both the backend and the frontend site, page plugin does not contain any UI.  Some of the uses of page plugin are:

  •     Get data from remote service for users on the Page.
  •     Submit a Page to add content OR send an email.
  •     Other requests.

It is ok to write the data access code on the layout OR views, but it is not recommended in the MVC pattern. A better solution for such cases is to use the Page Plug-in to write the logic code into a custom assembly, then upload it into Kooboo CMS and use it in Pages or Views.

It is easy to develop a Page Plug-in. We have released the VS Project Template to help developers create Page Plug-in projects more eaisly. The "PagePluginSample.cs" in the project template is the same Page Plug-in. The brief of developing a Page Plug-in is implementing the interface of "IPagePlugin".

 public interface IPagePlugin
    {
        string Description { get; }
 
        ActionResult Execute(Page_Context pageContext, PagePositionContext positionContext);

        ActionResult HttpGet(Page_Context context, PagePositionContext positionContext);
       ActionResult HttpPost(Page_Context context, PagePositionContext positionContext);

    }
 NOTE: The HttpGet and HttpPost are new methods in Kooboo CMS 4.0. They will be executed corresponding to the HttpMethod(Get and Post).

Developing Page Plug-ins

  •     Download Kooboo.CMS.PluginTemplate.vsi.
  •     Double click the Kooboo.CMS.PluginTemplate.vsi to install the project templates into Visual Studio.
  •     Create a Page plug-in project using the project template under "Visual C# -> Web". 

  •      Remove these three files: "AssemblyInitializer.cs","CustomContentEventSubscriber.cs","CustomSiteEventsSubscriber.cs".
  •     Rename the "PagePluginSample" as your own plug-in name.
  •     Write the logic code in the "Execute" method.


Using Page Plug-ins

  •     Upload assembly of the Page Plug-in into Kooboo CMS Site. NOTE: Please also upload the dependency assemblies of the plugin assembly, otherwise you will encounter an "assembly not found" message at runtime.

  • Add the plugin into the Page or View. NOTE: The execution sequence at runtime will be the same for you to add the plugin into either Page or View.

Add_page_plugin_into_page

 

The execution sequence of Page Plug-in

All the Page Plug-ins are added both in the Page and Views(The views added in the page, which are not using RenderView.) will be invoked in the controller. The sequence flow for the Page execution will be: Kooboo CMS Request Flow.jpg

The picture shows the Page Plug-ins will be invoked foremost the controller action.

  •     When the "Execute" method returns a NOT-Null ActionResult value, the controller action will be returned without running the following code. For example: The plug-in want to return a JsonResult, JavascriptResult, ContentResult,FileResult etc...
  •     When the "Execute" method returns a Null value, the controller action will continue to run the following and render the Page html. In this case, the developer can store the custom data into ViewData, which can be used in the Layout and Views.

 pageViewContext.ControllerContext.Controller.ViewBag.PluginData = "Hello plug-in";
 

    By default, the page plugin will be invoked in all types of http requests, but you can filter by the http method to limit it to run only on specific types of requests. e.g:

 
  if (pageViewContext.ControllerContext.RequestContext.HttpContext.Request.HttpMethod.ToUpper() =="POST")
  {
 
  }
 

 Built-in Page Plug-ins

There are three types of Page Plug-ins built into Kooboo CMS.

  •     AddTextContentPlugin, used to add content using the submission values.
  •     UpdateTextContentPlugin, used to update text content using the submission values.
  •     DeleteTextContentPlugin, used to delete text content.