Skip to content

Instantly share code, notes, and snippets.

@gregwiechec
Created June 6, 2016 12:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gregwiechec/aa59356f4d6a54a413bcd2bb154590e9 to your computer and use it in GitHub Desktop.
Save gregwiechec/aa59356f4d6a54a413bcd2bb154590e9 to your computer and use it in GitHub Desktop.
Rendering current page in ContentArea
using System.Collections.Generic;
using ContentAreaWithPreview.Models.Pages;
using EPiServer;
using EPiServer.Core;
using EPiServer.Core.Html.StringParsing;
using EPiServer.Security;
using EPiServer.Web;
namespace ContentAreaWithPreview.Plugin
{
public class FakeStringFragment : ContentFragment, ISecurable
{
private readonly PageData _page;
private readonly IContentRepository _contentRepository;
public ISecurable Securable { get; set; }
public FakeStringFragment(PageData page, IContentRepository contentRepository,
TemplateControlLoader templateControlLoader,
ISecuredFragmentMarkupGenerator securedFragmentMarkupGenerator,
IPublishedStateAssessor publishedStateAssessor, DisplayOptions displayOptions)
: base(
contentRepository, templateControlLoader, securedFragmentMarkupGenerator, displayOptions,
publishedStateAssessor, new Dictionary<string, object>())
{
_page = page;
this._contentRepository = contentRepository;
ContentLink = page.ContentLink;
}
public override IContent GetContent()
{
var newsPage = this._contentRepository.GetDefault<NewsPage>(ContentReference.StartPage);
newsPage.Name = "Lorem ipsum dolor sit amet";
newsPage.TeaserText =
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer vel volutpat mauris, quis convallis magna.";
newsPage.PageImage = new ContentReference(233);
return newsPage;
}
ISecurityDescriptor ISecurable.GetSecurityDescriptor()
{
return this._page.GetSecurityDescriptor();
}
}
}
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/MasterPages/Root.Master" AutoEventWireup="true" CodeBehind="NewsPageView.aspx.cs" Inherits="ContentAreaWithPreview.Plugin.NewsPageView" %>
<asp:Content ContentPlaceHolderID="HeaderContentRegion" runat="server">
<asp:PlaceHolder ID="Bundles" runat="server" />
<style type="text/css">
.row>div:first-child,
.row>div:last-child {
opacity: 0.3;
}
.row:hover>div:first-child,
.row:hover>div:last-child {
opacity: 1;
}
</style>
</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>
using System;
using System.Linq;
using System.Web;
using System.Web.UI;
using ContentAreaWithPreview.Models.Pages;
using EPiServer;
using EPiServer.Core;
using EPiServer.Core.Html.StringParsing;
using EPiServer.ServiceLocation;
using EPiServer.Shell.WebForms;
using EPiServer.Web;
namespace ContentAreaWithPreview.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.Fragments.Add(CreateFakeFragment(contentAreaItem));
newsPage.MainContentArea.Items.Add(contentAreaItem);
newsPage.MainContentArea.Fragments.Add(CreateFakeFragment(contentAreaItem));
this.CurrentContent = newsPage;
HttpContext.Current.Request.RequestContext.RouteData.DataTokens["contextmode"] = ContextMode.Edit;
}
private IStringFragment CreateFakeFragment(ContentAreaItem contentAreaItem)
{
var publishedStateAssessor = ServiceLocator.Current.GetInstance<IPublishedStateAssessor>();
var templateControlLoader = ServiceLocator.Current.GetInstance<TemplateControlLoader>();
var displayOptions = ServiceLocator.Current.GetInstance<DisplayOptions>();
var securedFragmentMarkupGeneratorFactory =
ServiceLocator.Current.GetInstance<ISecuredFragmentMarkupGeneratorFactory>();
var fragmentMarkupGenerator = securedFragmentMarkupGeneratorFactory.CreateSecuredFragmentMarkupGenerator();
// copied from current page item
fragmentMarkupGenerator.ContentGroup = contentAreaItem.ContentGroup;
if (contentAreaItem.AllowedRoles != null && Enumerable.Any<string>(contentAreaItem.AllowedRoles))
fragmentMarkupGenerator.RoleSecurityDescriptor.RoleIdentities = contentAreaItem.AllowedRoles;
return new FakeStringFragment((PageData) this.CurrentContent, this.ContentRepository,
templateControlLoader,
fragmentMarkupGenerator,
publishedStateAssessor,
displayOptions
);
}
}
}
using System.Web;
using ContentAreaWithPreview.Models.Pages;
using EPiServer.ServiceLocation;
using EPiServer.Shell;
namespace ContentAreaWithPreview.Business
{
[ServiceConfiguration(typeof (ViewConfiguration))]
public class NewsSeeAlsoView : ViewConfiguration<NewsPage>
{
public const string ViewKey = "news-page-view";
public NewsSeeAlsoView()
{
Key = ViewKey;
Name = "News preview";
Description = "Preview of news page";
ControllerType = "refreshableIframe/refreshablehIframeController";
ViewType = VirtualPathUtility.ToAbsolute("~/Plugin/NewsPageView.aspx");
IconClass = "epi-iconPreview";
}
}
}
define([
"dojo/_base/declare",
"epi-cms/widget/IFrameController"
],
function(
declare,
IFrameController
) {
return declare([IFrameController], {
_constructQuery: function (context) {
var result = this.inherited(arguments);
result.timeStamp = new Date().getTime();
return result;
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment