tedgustaf.se

Creating simple Dynamic Content in EPiServer CMS 6 R2

This post walks you through a simple example of how to create dynamic content in EPiServer CMS 6 R2 using the new DynamicContentPlugIn attribute.

  • 24 April 2011
  • 0

Dynamic Content is easier in EPiServer CMS 6 R2

In EPiServer CMS 6 R1 we created dynamic content by implementing the IDynamicContent interface (see this post on how to create dynamic content in CMS 6 R1).

The IDynamicContent interface still exists in CMS 6 R2, but we don’t have to use it – especially if we create dynamic content based on a user control, which is what we’ll do in this post.

Create a new user control

First of all we create a new user control…

image

…and make it inherit UserControlBase:

using System;
 
namespace EPiServer.Templates.AlloyTech.DynamicContent
{
    public partial class UserList : UserControlBase
    {
        
    }
}

Add the DynamicContentPlugIn attribute

Next we add the new DynamicContentPlugIn attribute to ensure EPiServer loads our user control as a dynamic content type on startup (we no longer need to register anything in web.config):

using System;
using EPiServer.DynamicContent;
 
namespace EPiServer.Templates.AlloyTech.DynamicContent
{
    [DynamicContentPlugIn(
        DisplayName = "User list",
        Description = "Displays a list of users in a specific role",
        ViewUrl ="~/Templates/AlloyTech/DynamicContent/UserList.ascx")]
    public partial class UserList : UserControlBase
    {
 
    }
}

Populate the user list based on the Dynamic Content settings

First, let’s add a Repeater to the code-front of our dynamic content user control:

<asp:Repeater ID="ListOfUsers" runat="server">
 
    <HeaderTemplate>
        <ul>
    </HeaderTemplate>
 
    <FooterTemplate>
        </ul>
    </FooterTemplate>
 
    <ItemTemplate>
        <li><%# Container.DataItem %></li>
    </ItemTemplate>
 
</asp:Repeater>

Next, let’s add some logic to populate the list to display all users in a specific role (which role will be specified by the web editor):

using System;
using System.Linq;
using System.Web.Security;
using EPiServer.DynamicContent;
 
namespace EPiServer.Templates.AlloyTech.DynamicContent
{
    [DynamicContentPlugIn(
        DisplayName = "User list",
        Description = "Displays a list of all users on the website",
        ViewUrl = "~/Templates/AlloyTech/DynamicContent/UserList.ascx")]
    public partial class UserList : UserControlBase
    {
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
 
            if (!string.IsNullOrEmpty(Role))
            {
                ListOfUsers.DataSource = Roles.GetUsersInRole(Role).Select(Membership.GetUser);
                ListOfUsers.DataBind();
            }
        }
 
        public string Role { get; set; }
    }
}

Now, let’s insert our User list on a page (note the Settings group where our public Role property has been included automatically):

image

EPiServer automatically takes all public properties (of compatible types) and renders controls for them in the Dynamic Content dialog.

Now this…

image

…gives us this:

image