smithsson68@gmail.com
Dec 20, 2010
  4984
(1 votes)

Dynamic Content Just Got Easier

EPiServer CMS 6 R2 includes some improvements to Dynamic Content (DC from now on) both for developers and editors.

Simplified Implementation

In previous versions of DC, you had to create a class that implemented the EPiServer.DynamicContent.IDynamicContent interface. You then needed to render the DC in some way. This could be done directly from the IDynamicContent.Render method or by returning a instance of a System.Web.UI.Control.

One of the developer improvements in CMS 6 R2 is to be able to take a web control and use it directly as the DC implementation. No need to worry about the IDynamicContent implementation any more!

The “fairy dust” that makes this possible is a new attribute called EPiServer.DynamicContent.DynamicContentPlugInAttribute. You decorate your DC web control with this and the framework will do the rest for you.

The attribute class has the following properties that can be set in your code:

public string ViewUrl

This should contain the Url of the control that implements the Dynamic Content (i.e. the control the attribute has been applied to).

As the DynamicContentPlugInAttribute derives from the existing EPiServer.PlugIn.GuiPlugInAttribute, the following properties are also available:

public string DisplayName
public string Description
public string Url
public string UrlFromUi
public string UrlFromUtil

One of the Url, UrlFromUi or UrlFromUtil properties may be set if you want to provide your own edit control for editors to create and update instances of your Dynamic Content. If none of these are set then the default EPiServer.UI.Edit.DynamicContentSettings control will be used.

The default edit control will provide a user interface for all properties on your DC control that meet the following criteria:

  • Have a public getter and setter
  • Is of type System.String, System.Boolean, System.Int32, EPiServer.Core.PageReference or derived from EPiServer.Core.PropertyData
  • Is an instance member
  • Is not inherited from a base class

Note that these editable properties will also be automatically serialized / de-serialized as the state of your DC instance. Since a DC is stored inline in a XHTML string, the state is encoded as a Base64 string which can make the state rather large. Keep this in mind and try to use as little state as possible for you DC.

The following example shows a version of Rating Dynamic Content Control first blogged about here.

   1:  [EPiServer.DynamicContent.DynamicContentPlugIn
   2:      (
   3:          DisplayName="Rating",
   4:          Description="Provides a rating facility for pages",
   5:          ViewUrl="/Templates/Public/Units/Placeable/RatingControl.ascx"
   6:      )]            
   7:      public partial class RatingControl : UserControlBase
   8:      {
   9:          private Rating _currentRating;
  10:          private const string PageObjectName = "Rating";
  11:   
  12:          /// <summary>
  13:          /// The text to display before the rating icons
  14:          /// </summary>
  15:          public string InstructionText
  16:          {
  17:              get;
  18:              set;
  19:          }
  20:   
  21:          /// <summary>
  22:          /// The text to display after the rating icons. 
  23:          /// If the string includes a format placeholder the average value will be inserted there
  24:          /// </summary>
  25:          public string PostRateText
  26:          {
  27:              get;
  28:              set;
  29:          }
  30:   
  31:          protected void Page_PreRender(object sender, EventArgs e)
  32:          {
  33:              // Display the average rating
  34:              if (_currentRating == null)
  35:              {
  36:                  PageObjectManager pom = new PageObjectManager(this.PageBase.CurrentPage);
  37:                  _currentRating = pom.Load<Rating>(PageObjectName);
  38:              }
  39:   
  40:              if (_currentRating != null)
  41:              {
  42:                  this.AverageRatingLabel.Text = string.Format(PostRateText, _currentRating.Average.ToString());
  43:              }
  44:              else
  45:              {
  46:                  // Not rated yet
  47:                  this.AverageRatingLabel.Text = string.Format(PostRateText, "0");
  48:              }
  49:          }
  50:   
  51:          protected void StarImage_Command(object sender, CommandEventArgs e)
  52:          {
  53:              // The command argument is the rating value
  54:              int value = int.Parse(e.CommandArgument.ToString());
  55:   
  56:              PageObjectManager pom = new PageObjectManager(this.PageBase.CurrentPage);
  57:   
  58:              // See if we already have a rating object for this page
  59:              _currentRating = pom.Load<Rating>(PageObjectName);
  60:   
  61:              if (_currentRating == null)
  62:              {
  63:                  _currentRating = new Rating();
  64:              }
  65:   
  66:              _currentRating.Rate(value);
  67:   
  68:              // And save
  69:              pom.Save(PageObjectName, _currentRating);
  70:          }       
  71:      }

No more web.config registration

Another nice feature in CMS 6 R2 is that Dynamic Content no longer needs to be registered in the web.config. Note that this only applies when the DynamicContentPlugInAttribute is used as an alternative.

The full source code for this example can be found here on EPiServer World Code.

The Beta version of EPiServer CMS 6 R2 can be found here.

Dec 20, 2010

Comments

Dec 20, 2010 05:03 PM

Great work. This will make it a'lot easier for developers to get started with dynamic content. Thanks!

Eric
Eric Dec 21, 2010 09:16 AM

This is awsome no i can stop use Allans old but great plugin :)

smithsson68@gmail.com
smithsson68@gmail.com Dec 22, 2010 05:36 PM

Just to add that even if you create Dynamic Content the old way by creating a class that implements IDynamicContent you can still use the DynamicContentPlugInAttribute on that class to avoid registering the DC in the web.config. Obviously, don't specify a ViewUrl if you don't render with a control.

In fact the section has been removed from the default web.config.

Please login to comment.
Latest blogs
Opti ID overview

Opti ID allows you to log in once and switch between Optimizely products using Okta, Entra ID, or a local account. You can also manage all your use...

K Khan | Jul 26, 2024

Getting Started with Optimizely SaaS using Next.js Starter App - Extend a component - Part 3

This is the final part of our Optimizely SaaS CMS proof-of-concept (POC) blog series. In this post, we'll dive into extending a component within th...

Raghavendra Murthy | Jul 23, 2024 | Syndicated blog

Optimizely Graph – Faceting with Geta Categories

Overview As Optimizely Graph (and Content Cloud SaaS) makes its global debut, it is known that there are going to be some bugs and quirks. One of t...

Eric Markson | Jul 22, 2024 | Syndicated blog

Integration Bynder (DAM) with Optimizely

Bynder is a comprehensive digital asset management (DAM) platform that enables businesses to efficiently manage, store, organize, and share their...

Sanjay Kumar | Jul 22, 2024

Frontend Hosting for SaaS CMS Solutions

Introduction Now that CMS SaaS Core has gone into general availability, it is a good time to start discussing where to host the head. SaaS Core is...

Minesh Shah (Netcel) | Jul 20, 2024

Optimizely London Dev Meetup 11th July 2024

On 11th July 2024 in London Niteco and Netcel along with Optimizely ran the London Developer meetup. There was an great agenda of talks that we put...

Scott Reed | Jul 19, 2024