How to create and install a Portlet

From Sense/Net Wiki
Jump to: navigation, search
  •  
  •  
  •  
  •  
  • 100%
  • 6.0
  • Enterprise
  • Community
  • Planned

Overview

One of the most commonly used features of Sense/Net ECMS for extending the base functionality with custom code is to develop portlets. Portlets are ASP.Net WebParts written in C# and appear as draggable boxes on the user interface. Most of the Sense/Net web pages are built up of portlets. In this tutorial you will learn how to create a new portlet from code, deploy it and show it on a page.

Steps

1. Create a new class

2. Implement your portlet code

Our portlet will display a welcome message and do nothing else. Here is the code:

using System.Web.UI;
using SenseNet.Portal.UI.PortletFramework;
 
namespace MyWebApplication.Code
{
    public class MyPortlet : PortletBase
    {
        public MyPortlet()
        {
            this.Name = "My Portlet";
            this.Description = "My first portlet.";
            this.Category = new PortletCategory(PortletCategoryType.Application);
        }
 
        protected override void OnInit(System.EventArgs e)
        {
            this.Controls.Add(new LiteralControl { Text = "Hello world" });
            base.OnInit(e);
        }
    }
}

This is the most basic portlet you can imagine. There are some compulsory parts:

  • must inherit from PortletBase (or its derivative) - consider using CacheablePortlet or ContextBoundPortlet,
  • in the constructor you must set the name, description and category.

For category you can also provide custom category with the following signature:

this.Category = new PortletCategory("My category", "Description of my category");

3. Compile code

If you are done with your code, compile it. If you chose option 2 when creating your class you also need to copy the compiled dlls into the webfolder's bin folder. If you used the standard Sense/Net development environment, you can skip this step, as your compiled dlls of your web application are already in the webfolder's bin. This step will make your web application automatically be stopped by the IIS and it will be restarted when you first send a request to the website.

4. Synchronize portlet

In order that we are able to add our new portlet to a page we first need to synchronize it in the Content Repository. The synchronize action will use the name, description and category properties given in the constructor of the portlet code to create a Portlet content in the content repository under /Root/Portlets. To synchronize portlets open the Content Explorer and navigate to /Root/Portlets. In the Actions menu click the Synchronize action:

Synchronizing portlets

A list of yet unsynchronized portlets will appear, including our new portlet. Click install portlets:

Synchronizing our new portlet

Our portlet will appear in the corresponding category under /Root/Portlets:

Portlet synchronized

Note, that once a portlet has been synchronized, from then onwards it will be available from the add dialog when adding portlets to pages. Thus, there is no need to re-synchronize portlets in case their code changes. It is enough to make sure that the new code is running in the webfolder's bin. However, if the name, description or category of the portlet changes, you will need to update the corresponding Portlet content in the repository, or delete it and re-synchronize your portlet afterwards.

5. Add portlet to a page

For detailed explanation of this step please refer to How to add a portlet to a page

Let's pick a page of your choice or create a new one. In this example we will use /Root/Sites/Default_Site/features/content, which is available using the url http://localhost/features/content. Let's browse this page and use the PRC to switch to edit mode. Click on Add portlet at the Wide column zone: our new portlet should appear under the corresponding category:

Add our portlet to a page

After clicking 'Ok' our portlet should appear in the selected zone:

Our portlet on the page

Related links

References