Anders Hattestad
Jun 28, 2013
  6310
(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
Optimizely finally releases new and improved list properties!

For years, the Generic PropertyList has been widely used, despite it being unsupported. Today a better option is released!

Tomas Hensrud Gulla | Mar 28, 2023 | Syndicated blog

Official List property support

Introduction Until now users were able to store list properties in three ways: Store simple types (int, string, DateTime, double) as native...

Bartosz Sekula | Mar 28, 2023

New dashboard implemented in CMS UI 12.18.0

As part of the CMS UI 12.18.0 release , a new dashboard has been added as a ‘one stop shop’ to enable editors to access all of their content items,...

Matthew Slim | Mar 28, 2023

How to Merge Anonymous Carts When a Customer Logs In with Optimizely Commerce 14

In e-commerce, it is common for users to browse a site anonymously, adding items to their cart without creating an account. Later, when the user...

Francisco Quintanilla | Mar 27, 2023