Anders Hattestad
May 8, 2013
visibility 3894
star star star star star
(4 votes)

Render ContentArea with temporarily content

One nice feature in EPiServer 7 is the ability to render content based on tags and type.  But sometimes you want to add temporary items to a collection and then render them. This can be a RSS feed, or if you want to add one link list with and existing ContentArea.

The problem is that temporary items will not be rendered thru the PropertyContentAreaControl. So if you add some temporarily items like some RSS articles it will not render.

You can add IContent to a ContentArea, but internally it is stored as content reference, and the object is not preserved. It basically add the content Reference ID, and then when you render it it gets the content again. That means you cant manipulate the IContent items before you render them. Personally I think this behaviour is a bit odd, and makes it a bit harder to do cool stuff Smilefjes

Create temporarily content

Before it was possible to create pages and add them to a page list. You had to add the ACL to the page so it was not filtered out.

Code Snippet
  1. var page = new TempPageType();
  2. page.Property.Add("PageTypeID", new PropertyNumber(pageID));
  3. page.Property.Add("PageLink", new PropertyPageReference(PageReference.EmptyReference));
  4.  
  5. page.Property.Add("PagePendingPublish", new PropertyBoolean(false));
  6. page.Property.Add("PageWorkStatus",
  7.                     new PropertyNumber(Convert.ToInt32(VersionStatus.Published)));
  8. page.ACL.Add(new AccessControlEntry("Everyone", AccessLevel.Read));

 

In EPiServer 7 you have to register this page type and also add the PageTypeID to the temporarily page

Code Snippet
  1. var repository = ServiceLocator.Current.GetInstance<IContentTypeRepository<PageType>>();
  2. var pageType = repository.Load(typeof(TempPageType));
  3. var pageID=-1;
  4. if (pageType!=null)
  5.     pageID=pageType.ID;
  6. page.Property.Add("PageTypeID", new PropertyNumber(pageID));

And then register the page type.

 

Code Snippet
  1. [ContentType(GUID = "454E47D6-BC76-4596-BC03-8BA519BB4650", GroupName = "Default",AvailableInEditMode=false)]
  2. public class TempPageType : SitePageData
  3. {
  4.     [Ignore]
  5.     public virtual object InnerObject { get; set; }
  6.  
  7.     public override EPiServer.Security.ISecurityDescriptor GetSecurityDescriptor()
  8.     {
  9.         if (PageReference.IsNullOrEmpty(PageLink))
  10.             return new OwnSecurityDescriptor();
  11.         return base.GetSecurityDescriptor();
  12.     }
  13.        
  14. }
  15.   public classOwnSecurityDescriptor:ISecurityDescriptor
  16. {
  17.     public AccessLevel GetAccessLevel(IPrincipal principal)
  18.     {
  19.         return AccessLevel.FullAccess;
  20.     }
  21.     public bool HasAccess(IPrincipal principal, AccessLevel access)
  22.     {
  23.         return true;
  24.     }
  25. }

 

 

To render

The solution is to make your own render of a List of IContent

Lucky for us it’s still easy to look at the current code using Reflector or some other app.

This code will take a list of IContent and render them accordingly to the tags. So you can use it like this, where Result is a List<IContent>

Code Snippet
  1.   publicbool AddResult2Container(Control container,  string tag, bool add_Ul_Li)
  2. {
  3.     var haveContent = false;
  4.     var result = new RenderContentArea() { Data = Result };
  5.     result.Tag = tag;
  6.     if (add_Ul_Li)
  7.     {
  8.         result.TagName = "ul";
  9.         result.TagChildName = "li";
  10.     }
  11.     container.Controls.Add(result);
  12.     haveContent = result.CreateContent();
  13.     return haveContent;
  14. }

The code that runs is this

Code Snippet
  1. public class RenderContentArea : PlaceHolder
  2. {
  3.     public List<IContent> Data { get; set; }
  4.     public string TagName { get; set; }
  5.     public string TagChildName { get; set; }
  6.  
  7.     public string ItemCssClass { get; set; }
  8.     public string ItemChildCssClass { get; set; }
  9.  
  10.     public string Tag { get; set; }
  11.  
  12.     private ContentControlResolver _contentControlResolver;
  13.     public ContentControlResolver ContentControlResolver
  14.     {
  15.         get
  16.         {
  17.             ContentControlResolver arg_1D_0;
  18.             if ((arg_1D_0 = this._contentControlResolver) == null)
  19.             {
  20.                 arg_1D_0 = (this._contentControlResolver = ServiceLocator.Current.GetInstance<ContentControlResolver>());
  21.             }
  22.             return arg_1D_0;
  23.         }
  24.         set
  25.         {
  26.             this._contentControlResolver = value;
  27.         }
  28.     }
  29.  
  30.     protected IList<Control> GetContentRenderers2()
  31.     {
  32.         if (Data == null || Data.Count<IContent>() == 0)
  33.         {
  34.             return new List<Control>();
  35.         }
  36.         string itemTagName = TagChildName;
  37.         string itemCssClass = ItemChildCssClass;
  38.  
  39.         return ResolveContentControls(this, this.Tag, itemCssClass, itemTagName);
  40.     }
  41.     public virtual Injected<TemplateControlLoader> TemplateControlLoader
  42.     {
  43.         get;
  44.         set;
  45.     }
  46.     private WebFormsContentValidator _contentValidator;
  47.     public virtual WebFormsContentValidator ContentValidator
  48.     {
  49.         get
  50.         {
  51.             WebFormsContentValidator arg_1D_0;
  52.             if ((arg_1D_0 = this._contentValidator) == null)
  53.             {
  54.                 arg_1D_0 = (this._contentValidator = ServiceLocator.Current.GetInstance<WebFormsContentValidator>());
  55.             }
  56.             return arg_1D_0;
  57.         }
  58.         set
  59.         {
  60.             this._contentValidator = value;
  61.         }
  62.     }
  63.     public virtual IList<Control> ResolveContentControls( Control parentControl, string tag, string itemCssClass, string itemTagName)
  64.     {
  65.  
  66.         IContentFilter filter = new FilterContentForVisitor(TemplateTypeCategories.WebFormsPartial, tag);
  67.         List<Control> list = new List<Control>();
  68.         IEnumerable<IContent> source = Data;
  69.         IEnumerable<IContent> enumerable =
  70.             from c in source
  71.             where  !filter.ShouldFilter(c)
  72.             select c;
  73.         foreach (IContent current in enumerable)
  74.         {
  75.             Control control = this.TemplateControlLoader.Service.LoadControl(HttpContext.Current.ContextBaseOrNull(), current, parentControl.TemplateControl, tag);
  76.             if (this.ContentValidator.ExistInControlHierarchy(parentControl, current, control))
  77.             {
  78.                 //if (enableEditFeatures)
  79.                 //{
  80.                 //    HtmlGenericControl item = this.BuildCircularReferenceControl(current, itemTagName, itemCssClass);
  81.                 //    list.Add(item);
  82.                 //}
  83.             }
  84.             else
  85.             {
  86.                 IVersionable versionable = current as IVersionable;
  87.                 if (versionable == null || versionable.Status == VersionStatus.Published)
  88.                 {
  89.                     ContentRenderer item2 = new ContentRenderer
  90.                     {
  91.                         CurrentControl = control,
  92.                         CurrentData = current,
  93.                         CurrentContent = current,
  94.                         Tag = tag,
  95.                         CustomTagName = itemTagName,
  96.                         CssClass = itemCssClass,
  97.                         TemplateControlLoader = this.TemplateControlLoader,
  98.                         RenderType =RenderType.Default
  99.                     };
  100.                     list.Add(item2);
  101.                 }
  102.             }
  103.         }
  104.         return list;
  105.     }
  106.       
  107.         
  108.     public  bool CreateContent()
  109.     {
  110.           
  111.         string containerTagName = (!string.IsNullOrEmpty(TagName)) ? TagName : "div";
  112.         System.Collections.Generic.IList<Control> contentRenderers = this.GetContentRenderers2();
  113.         Control control = CreateMainContainer(containerTagName);
  114.         foreach (Control current in contentRenderers)
  115.         {
  116.             control.Controls.Add(current);
  117.             ContentRenderer contentRenderer = current as ContentRenderer;
  118.             if (contentRenderer != null)
  119.             {
  120.                 contentRenderer.EnsureChildControlsCreated();
  121.             }
  122.         }
  123.         if (contentRenderers.Count > 0)
  124.             return true;
  125.         return false;
  126.          
  127.     }
  128.     protected HtmlGenericControl CreateMainContainer( string containerTagName)
  129.     {
  130.         HtmlGenericControl htmlGenericControl = new HtmlGenericControl(containerTagName);
  131.         this.Controls.Add(htmlGenericControl);
  132.            
  133.         //if (enableEditFeatures)
  134.         //{
  135.         //    base.ApplyEditAttributes();
  136.         //}
  137.         return htmlGenericControl;
  138.     }
  139.         
  140. }
May 08, 2013

Comments

error Please login to comment.
Latest blogs
Exploring Asset Lifecycle Management Approaches for Bynder and Optimizely SaaS CMS

Note: This is Part 3 of our Bynder integration series. For setup and filtering prerequisites, see Part 1  and  Part 2 . Introduction In my previous...

Vipin Banka | Jul 5, 2026

Unlock AI-Ready Experiences with Optimizely

Over the past few months, almost every customer conversation has shifted from SEO to AI readiness. The questions are no longer just: “How do we......

Madhu | Jul 5, 2026 |

Planning Your Bynder DAM and Optimizely SaaS CMS Integration the Right Way: Avoiding Asset Sprawl and Unnecessary Synchronization

Note: This is Part 2 of our Bynder integration series. If you missed the Part 1, check out " Implementing the Bynder DAM Connector with Optimizely...

Vipin Banka | Jul 4, 2026

Implementing the Bynder DAM Connector with Optimizely SaaS CMS: Lessons Learned

What I learned while integrating Bynder DAM with Optimizely SaaS CMS, exploring Optimizely Graph, and building a headless frontend experience....

Vipin Banka | Jul 3, 2026

Optimizely London developer meetup 2026: a round up

Well, what can I say? Last night we wrapped up! Yet another London Developer Meetup, hosted at the superb Lightwell venue And this is also a...

Scott Reed | Jul 3, 2026

AvantiBit Custom Settings for Optimizely CMS

AvantiBit Custom Settings is a free, Apache-2.0 Optimizely CMS add-on for typed, site- and language-aware configuration that stays out of content...

Enes Bajramovic | Jul 3, 2026 |