logo

Creating portlet page with a portlet when activating module container

In this example, when the container module is activated (all modules and extension points are loaded), a new portlet page is created, and the portlet is added to the page. The module container is activated when you start the application or re-create the container (when you activate/deactivate the module).

Example of the data display

portlet page with a portlet when activating a module container

Fig. 1. A portlet page with a portlet when activating a module container

Extension methods (interface)

An extension point (interface) IModuleContainerEvents has the following methods:

/// <summary>
/// The container is activated (all modules and extension points are loaded). 
/// Called after the activation of the container.
/// The container is activated:
/// 1) when the application starts
/// 2) when re-creating the container (when you activate/deactivate the module)
/// </summary>
void Activated();
         
/// <summary>
/// The container is disposed (called after the module restart)
/// </summary>
void Terminating();

Example of the extension point class

[Component]
public class ModuleContainerEvents : IModuleContainerEvents
{
    private const string UidS = "{159E131B-1F56-46ad-B31D-2A6EE573C9CF}";
    public static Guid UID = new Guid(UidS); // Guid of the portlet page
 
    public IEntityManager<IPortletPage> PageManager { get; set; }
    public PortletManager PortletManager { get; set; }
    public ISecurityService SecurityService { get; set; }
    public UserManager UserManager { get; set; }
    public IUnitOfWorkManager UnitOfWorkManager { get; set; }
    public TwoColumn5050Layout Layout { get; set; }
 
 
    public void Activated()
    {
        var admin = UserManager.Load(SecurityConstants.AdminUserUid);
        var pagecount = PageManager.Find(a => a.Uid == UID).Count;
        if (pagecount == 0)
        {
            SecurityService.RunByUser(admin,
                () =>
                    CreateDefaultPage(admin, UID,
                        "My portlet page"));
        }
    }
 
    public void Terminating()
    {
 
    }
 
    public virtual void CreateDefaultPage(EleWise.ELMA.Security.Models.IUser admin, Guid portletPageGuid,
        string portletPageName)
    {
        UnitOfWorkManager.Execute(delegate
        {
            //Creating a protlet page
            var portletPage = PageManager.Create();
            portletPage.Uid = portletPageGuid;
            portletPage.Name = portletPageName;
            portletPage.CreationAuthor = admin;
            portletPage.PortletsLayoutId = Layout.Id;
            PageSecurity.GrandAllUsersViewPermission(portletPage);
            portletPage.Save();
 
 
            SecurityService.RunWithElevatedPrivilegies(delegate
            {
                var portalPageDataPath = PortletsRestrictionProvider.PortalPageDataPath(portletPage);
                var sharedPortletManager = PortletManager.Create(PersonalizationScope.Shared, portalPageDataPath);
                     
                var portlet = PortletManager.GetPortlet(MyPortlet.UID);
                var zone = Layout.LeftZone.Id;
                //Adding a portlet to the page
                var portletPersonalization = sharedPortletManager.AddPortlet(portlet, portalPageDataPath);
                portletPersonalization.Zone = zone;
                portletPersonalization.Frame = PortletFrame.Full; 
                sharedPortletManager.SavePersonalization(portletPersonalization, portalPageDataPath);
            });
        });
    }
}

In this example, we have added a portlet on a page created in the CreateDefaultPage method. 

Links to API elements

IModuleContainerEvents