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
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.
- var page = new TempPageType();
- page.Property.Add("PageTypeID", new PropertyNumber(pageID));
- page.Property.Add("PageLink", new PropertyPageReference(PageReference.EmptyReference));
- page.Property.Add("PagePendingPublish", new PropertyBoolean(false));
- page.Property.Add("PageWorkStatus",
- new PropertyNumber(Convert.ToInt32(VersionStatus.Published)));
- 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
- var repository = ServiceLocator.Current.GetInstance<IContentTypeRepository<PageType>>();
- var pageType = repository.Load(typeof(TempPageType));
- var pageID=-1;
- if (pageType!=null)
- pageID=pageType.ID;
- page.Property.Add("PageTypeID", new PropertyNumber(pageID));
And then register the page type.
- [ContentType(GUID = "454E47D6-BC76-4596-BC03-8BA519BB4650", GroupName = "Default",AvailableInEditMode=false)]
- public class TempPageType : SitePageData
- {
- [Ignore]
- public virtual object InnerObject { get; set; }
- public override EPiServer.Security.ISecurityDescriptor GetSecurityDescriptor()
- {
- if (PageReference.IsNullOrEmpty(PageLink))
- return new OwnSecurityDescriptor();
- return base.GetSecurityDescriptor();
- }
- }
- public classOwnSecurityDescriptor:ISecurityDescriptor
- {
- public AccessLevel GetAccessLevel(IPrincipal principal)
- {
- return AccessLevel.FullAccess;
- }
- public bool HasAccess(IPrincipal principal, AccessLevel access)
- {
- return true;
- }
- }
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>
- publicbool AddResult2Container(Control container, string tag, bool add_Ul_Li)
- {
- var haveContent = false;
- var result = new RenderContentArea() { Data = Result };
- result.Tag = tag;
- if (add_Ul_Li)
- {
- result.TagName = "ul";
- result.TagChildName = "li";
- }
- container.Controls.Add(result);
- haveContent = result.CreateContent();
- return haveContent;
- }
The code that runs is this
- public class RenderContentArea : PlaceHolder
- {
- public List<IContent> Data { get; set; }
- public string TagName { get; set; }
- public string TagChildName { get; set; }
- public string ItemCssClass { get; set; }
- public string ItemChildCssClass { get; set; }
- public string Tag { get; set; }
- private ContentControlResolver _contentControlResolver;
- public ContentControlResolver ContentControlResolver
- {
- get
- {
- ContentControlResolver arg_1D_0;
- if ((arg_1D_0 = this._contentControlResolver) == null)
- {
- arg_1D_0 = (this._contentControlResolver = ServiceLocator.Current.GetInstance<ContentControlResolver>());
- }
- return arg_1D_0;
- }
- set
- {
- this._contentControlResolver = value;
- }
- }
- protected IList<Control> GetContentRenderers2()
- {
- if (Data == null || Data.Count<IContent>() == 0)
- {
- return new List<Control>();
- }
- string itemTagName = TagChildName;
- string itemCssClass = ItemChildCssClass;
- return ResolveContentControls(this, this.Tag, itemCssClass, itemTagName);
- }
- public virtual Injected<TemplateControlLoader> TemplateControlLoader
- {
- get;
- set;
- }
- private WebFormsContentValidator _contentValidator;
- public virtual WebFormsContentValidator ContentValidator
- {
- get
- {
- WebFormsContentValidator arg_1D_0;
- if ((arg_1D_0 = this._contentValidator) == null)
- {
- arg_1D_0 = (this._contentValidator = ServiceLocator.Current.GetInstance<WebFormsContentValidator>());
- }
- return arg_1D_0;
- }
- set
- {
- this._contentValidator = value;
- }
- }
- public virtual IList<Control> ResolveContentControls( Control parentControl, string tag, string itemCssClass, string itemTagName)
- {
- IContentFilter filter = new FilterContentForVisitor(TemplateTypeCategories.WebFormsPartial, tag);
- List<Control> list = new List<Control>();
- IEnumerable<IContent> source = Data;
- IEnumerable<IContent> enumerable =
- from c in source
- where !filter.ShouldFilter(c)
- select c;
- foreach (IContent current in enumerable)
- {
- Control control = this.TemplateControlLoader.Service.LoadControl(HttpContext.Current.ContextBaseOrNull(), current, parentControl.TemplateControl, tag);
- if (this.ContentValidator.ExistInControlHierarchy(parentControl, current, control))
- {
- //if (enableEditFeatures)
- //{
- // HtmlGenericControl item = this.BuildCircularReferenceControl(current, itemTagName, itemCssClass);
- // list.Add(item);
- //}
- }
- else
- {
- IVersionable versionable = current as IVersionable;
- if (versionable == null || versionable.Status == VersionStatus.Published)
- {
- ContentRenderer item2 = new ContentRenderer
- {
- CurrentControl = control,
- CurrentData = current,
- CurrentContent = current,
- Tag = tag,
- CustomTagName = itemTagName,
- CssClass = itemCssClass,
- TemplateControlLoader = this.TemplateControlLoader,
- RenderType =RenderType.Default
- };
- list.Add(item2);
- }
- }
- }
- return list;
- }
- public bool CreateContent()
- {
- string containerTagName = (!string.IsNullOrEmpty(TagName)) ? TagName : "div";
- System.Collections.Generic.IList<Control> contentRenderers = this.GetContentRenderers2();
- Control control = CreateMainContainer(containerTagName);
- foreach (Control current in contentRenderers)
- {
- control.Controls.Add(current);
- ContentRenderer contentRenderer = current as ContentRenderer;
- if (contentRenderer != null)
- {
- contentRenderer.EnsureChildControlsCreated();
- }
- }
- if (contentRenderers.Count > 0)
- return true;
- return false;
- }
- protected HtmlGenericControl CreateMainContainer( string containerTagName)
- {
- HtmlGenericControl htmlGenericControl = new HtmlGenericControl(containerTagName);
- this.Controls.Add(htmlGenericControl);
- //if (enableEditFeatures)
- //{
- // base.ApplyEditAttributes();
- //}
- return htmlGenericControl;
- }
- }
Comments