In previous article I started preparing custom iframe view. For the news page I displayed teaser text. In this post I will describe how to render current page in ContentArea.
In the template I used property control <EPiServer:Property PropertyName=”MainContentArea” runat=”server”/>. The control will render MainContentArea property value for currently edited page. I wanted to render currently edited page as a MainContentArea item.
To make it work, in code-behind OnInit mehtod I created a new instance of News Page.
1 2 3 4 5 6 |
//... var newsPage = contentRepository.GetDefault<NewsPage>(this.CurrentContent.ParentLink); newsPage.Name = "Preview"; //... |
Then added CurrentPage to MainContentArea:
1 2 3 4 5 6 7 |
//... newsPage.MainContentArea = new ContentArea(); var contentAreaItem = new ContentAreaItem { ContentLink = this.CurrentContent.ContentLink }; newsPage.MainContentArea.Items.Add(contentAreaItem); //... |
And then replaced current page with fake news page:
1 2 3 4 5 |
//... this.CurrentContent = newsPage; //... |
But when I run the application, the item was not renderred.
After debugging the code it turned out that page was not in edit mode and that’s why item was filtered out from collection of ContentArea items.
The only way to change editing mode (which I found) was to set contextmode variable in DataTokens.
1 2 3 4 5 |
//... HttpContext.Current.Request.RequestContext.RouteData.DataTokens["contextmode"] = ContextMode.Edit; //... |
When run the code once again, rendering start working.
Below is the full code-behind source code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
using System; using System.Web; using System.Web.UI; using EPiServer; using EPiServer.Core; using EPiServer.ServiceLocation; using EPiServer.Shell.WebForms; using EPiServer.Web; namespace AlloyDemo.Plugin { public partial class NewsPageView : ContentWebFormsBase { protected void Page_Load(object sender, EventArgs e) { } protected override void OnInit(EventArgs e) { base.OnInit(e); Bundles.Controls.Add(new LiteralControl(System.Web.Optimization.Scripts.Render("~/bundles/js").ToHtmlString())); Bundles.Controls.Add(new LiteralControl(System.Web.Optimization.Styles.Render("~/bundles/css").ToHtmlString())); var contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>(); var newsPage = contentRepository.GetDefault<NewsPage>(this.CurrentContent.ParentLink); newsPage.Name = "Preview"; newsPage.MainContentArea = new ContentArea(); var contentAreaItem = new ContentAreaItem { ContentLink = this.CurrentContent.ContentLink }; newsPage.MainContentArea.Items.Add(contentAreaItem); this.CurrentContent = newsPage; HttpContext.Current.Request.RequestContext.RouteData.DataTokens["contextmode"] = ContextMode.Edit; } } } |
and markup
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/MasterPages/Root.Master" AutoEventWireup="true" CodeBehind="NewsPageView.aspx.cs" Inherits="AlloyDemo.Plugin.NewsPageView" %> <asp:Content ContentPlaceHolderID="HeaderContentRegion" runat="server"> <asp:PlaceHolder ID="Bundles" runat="server" /> </asp:Content> <asp:Content ContentPlaceHolderID="FullRegion" runat="server"> <div class="container"> <h4 class="header">See also</h4> <EPiServer:Property PropertyName="MainContentArea" CssClass="row" runat="server"> <RenderSettings Tag="span8"></RenderSettings> </EPiServer:Property> </div> </asp:Content> |
In next post I will show how to add fake items to ContentArea.