World is now on Opti ID! Learn more

Anders Hattestad
Jun 28, 2013
  4106
(0 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
Make Global Assets Site- and Language-Aware at Indexing Time

I had a support case the other day with a question around search on global assets on a multisite. This is the result of that investigation. This co...

dada | Jun 26, 2025

The remote server returned an error: (400) Bad Request – when configuring Azure Storage for an older Optimizely CMS site

How to fix a strange issue that occurred when I moved editor-uploaded files for some old Optimizely CMS 11 solutions to Azure Storage.

Tomas Hensrud Gulla | Jun 26, 2025 |

Enable Opal AI for your Optimizely products

Learn how to enable Opal AI, and meet your infinite workforce.

Tomas Hensrud Gulla | Jun 25, 2025 |

Deploying to Optimizely Frontend Hosting: A Practical Guide

Optimizely Frontend Hosting is a cloud-based solution for deploying headless frontend applications - currently supporting only Next.js projects. It...

Szymon Uryga | Jun 25, 2025

World on Opti ID

We're excited to announce that world.optimizely.com is now integrated with Opti ID! What does this mean for you? New Users:  You can now log in wit...

Patrick Lam | Jun 22, 2025

Avoid Scandinavian Letters in File Names in Optimizely CMS

Discover how Scandinavian letters in file names can break media in Optimizely CMS—and learn a simple code fix to automatically sanitize uploads for...

Henning Sjørbotten | Jun 19, 2025 |