Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Anders Hattestad
Jun 28, 2013
  6834
(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
Solving the mystery of high memory usage

Sometimes, my work is easy, the problem could be resolved with one look (when I’m lucky enough to look at where it needs to be looked, just like th...

Quan Mai | Apr 22, 2024 | Syndicated blog

Search & Navigation reporting improvements

From version 16.1.0 there are some updates on the statistics pages: Add pagination to search phrase list Allows choosing a custom date range to get...

Phong | Apr 22, 2024

Optimizely and the never-ending story of the missing globe!

I've worked with Optimizely CMS for 14 years, and there are two things I'm obsessed with: Link validation and the globe that keeps disappearing on...

Tomas Hensrud Gulla | Apr 18, 2024 | Syndicated blog

Visitor Groups Usage Report For Optimizely CMS 12

This add-on offers detailed information on how visitor groups are used and how effective they are within Optimizely CMS. Editors can monitor and...

Adnan Zameer | Apr 18, 2024 | Syndicated blog