SaaS CMS has officially launched! Learn more now.

Anders Hattestad
Jun 28, 2013
  6878
(7 votes)

How to make automatically preview of an IContent item in other blocks

It is possible in EPiServer 7 to make a preview page, where you can edit the blocks properties and show how it will be displayed in different settings. But some times you are making a block that will be displayed in other blocks, and you want to show how  the current block will be shown in those.

You could make some logic in the preview template that takes care of this, but if you want to make it automatically this is a way to archive that.

If you are using the Alloy template it’s the /Views/Blocks/BlockPreview.aspx file you should change.

I change the front code to this

Code Snippet
  1. <asp:Content ID="Content1" ContentPlaceHolderID="PageContent" runat="server">
  2.     <EPiServer:Property  ID="EditProperty" runat="server">
  3.         <RenderSettings  EnableEditFeaturesForChildren="true" Tag="Edit" />
  4.     </EPiServer:Property>
  5.   <asp:PlaceHolder ID="PreviewArea" runat="server" />
  6. </asp:Content>

As you can see I have made a tag for Edit, so my edit view will be used for the current block.

The code behind looks like this

Code Snippet
  1. [TemplateDescriptor(Inherited = true, Tags = new[] { RenderingTags.Preview })]
  2. public partial class ItemPreviewControl : PreviewPage, IRenderTemplate<SiteItemBlockData>
  3. {
  4.     protected override void OnInit(EventArgs e)
  5.     {
  6.         base.OnInit(e);
  7.         //(Master as ResponsivtDesign).BodyClass = "";
  8.         EditProperty.DataBind();
  9.         ContentAPI.Current.CreatePreiviewOfItemInLists(PreviewArea, CurrentData,this);
  10.         RenderBlockPreviews();
  11.     }
  12.     protected override void OnSaveStateComplete(EventArgs e)
  13.     {
  14.         base.OnSaveStateComplete(e);
  15.         SetupPreviewPropertyControl(EditProperty, new[] { CurrentData });
  16.            
  17.     }
  18.     private void RenderBlockPreviews()
  19.     {
  20.         SetupPreviewPropertyControl(EditProperty, new[] { CurrentData });
  21.     }
  22.     private void SetupPreviewPropertyControl(Property propertyControl, IEnumerable<IContent> contents)
  23.     {
  24.         var contentArea = new ContentArea();
  25.         foreach (var content in contents)
  26.         {
  27.             contentArea.Add(content);
  28.         }
  29.         var previewProperty = new PropertyContentArea { Value = contentArea, Name = "PreviewPropertyData" };
  30.         propertyControl.InnerProperty = previewProperty;
  31.     }      
  32. }

The code that makes the preview are in the ContentAPI class. You could use that class for preview of pages in different kinds of settings also.

The code that finds all the blocks that is defined and checks if the block should display is like this

Code Snippet
  1. public class ContentAPI
  2. {
  3.     public static ContentAPI Current = new ContentAPI();
  4.         
  5.     public void CreatePreiviewOfItemInLists(Control container, IContent data,TemplateControl  templateControl)
  6.     {
  7.         var repository = ServiceLocator.Current.GetInstance<BlockTypeRepository>();
  8.         foreach (var block in repository.List())
  9.         {
  10.             var blockType = block.ModelType;
  11.             if (blockType.GetInterface(typeof(ICanBeUsedForPewivewOfItems).Name)!=null)
  12.             {
  13.                 var obj = EPiServer.DataFactory.Instance.GetDefault<IContent>(ContentReference.GlobalBlockFolder, block.ID);
  14.                 if (obj is ICanBeUsedForPewivewOfItems)
  15.                 {
  16.                     var add=(obj as ICanBeUsedForPewivewOfItems).AddInPreview(data);
  17.                     if (add)
  18.                     {
  19.                         Control control = this.TemplateControlLoader.Service.LoadControl(HttpContext.Current.ContextBaseOrNull(), obj, templateControl, "Default");
  20.                         container.Controls.Add(new Literal() { Text = "<div class='preview'><h2>" + block.DisplayName + " [" + block.Name + "]" + "</h2>" });
  21.                         container.Controls.Add(control);
  22.                         container.Controls.Add(new Literal() { Text = "</div>" });
  23.                     }
  24.                 }
  25.             }
  26.         }
  27.     }
  28.     public virtual Injected<TemplateControlLoader> TemplateControlLoader
  29.     {
  30.         get;
  31.         set;
  32.     }
  33. }

What this code do is that it checks every block if it implements the interface ICanBeUsedForPewivewOfItems.

Code Snippet
  1. public interface ICanBeUsedForPewivewOfItems
  2. {
  3.     bool AddInPreview(IContent item);
  4. }

And returns true if the current item could or should be displayed in that block view.

This will result in

image

 

This can also be used for a page, and show how it will appear in different kind of blocks.

if you add in your aspx or masterpage

Code Snippet
  1. <asp:Panel ID="PreviewStuff" runat="server" Visible="false">
  2.     <input type="button" onclick="$('#preivewItem').toggle();" value="Show page in different settings" />
  3.     <div id="preivewItem" style="display:none;">
  4.         <asp:PlaceHolder ID="PreviewArea" runat="server" />
  5.     </div>
  6. </asp:Panel>

and this to your code behind

Code Snippet
  1. protected override void OnLoad(EventArgs e)
  2. {
  3.     if (EPiServer.Editor.PageEditing.PageIsInEditMode)
  4.     {
  5.         if (PreviewArea != null && PreviewStuff!=null)
  6.         {
  7.             PreviewStuff.Visible = true;
  8.             ContentAPI.Current.CreatePreiviewOfItemInLists(PreviewArea, CurrentPage, this);
  9.         }
  10.     }
  11.     base.OnLoad(e);
  12. }

 

this will give you

image

image

Thats all. Hope you all a nice summerSmilefjes som blunker

Jun 28, 2013

Comments

Please login to comment.
Latest blogs
A day in the life of an Optimizely Developer - London Meetup 2024

Hello and welcome to another instalment of A Day In The Life Of An Optimizely Developer. Last night (11th July 2024) I was excited to have attended...

Graham Carr | Jul 16, 2024

Creating Custom Actors for Optimizely Forms

Optimizely Forms is a powerful tool for creating web forms for various purposes such as registrations, job applications, surveys, etc. By default,...

Nahid | Jul 16, 2024

Optimizely SaaS CMS Concepts and Terminologies

Whether you're a new user of Optimizely CMS or a veteran who have been through the evolution of it, the SaaS CMS is bringing some new concepts and...

Patrick Lam | Jul 15, 2024

How to have a link plugin with extra link id attribute in TinyMce

Introduce Optimizely CMS Editing is using TinyMce for editing rich-text content. We need to use this control a lot in CMS site for kind of WYSWYG...

Binh Nguyen Thi | Jul 13, 2024

Create your first demo site with Optimizely SaaS/Visual Builder

Hello everyone, We are very excited about the launch of our SaaS CMS and the new Visual Builder that comes with it. Since it is the first time you'...

Patrick Lam | Jul 11, 2024

Integrate a CMP workflow step with CMS

As you might know Optimizely has an integration where you can create and edit pages in the CMS directly from the CMP. One of the benefits of this i...

Marcus Hoffmann | Jul 10, 2024