November Happy Hour will be moved to Thursday December 5th.

Anders Hattestad
Jun 28, 2013
  6978
(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 SaaS CMS + Coveo Search Page

Short on time but need a listing feature with filters, pagination, and sorting? Create a fully functional Coveo-powered search page driven by data...

Damian Smutek | Nov 21, 2024 | Syndicated blog

Optimizely SaaS CMS DAM Picker (Interim)

Simplify your Optimizely SaaS CMS workflow with the Interim DAM Picker Chrome extension. Seamlessly integrate your DAM system, streamlining asset...

Andy Blyth | Nov 21, 2024 | Syndicated blog

Optimizely CMS Roadmap

Explore Optimizely CMS's latest roadmap, packed with developer-focused updates. From SaaS speed to Visual Builder enhancements, developer tooling...

Andy Blyth | Nov 21, 2024 | Syndicated blog

Set Default Culture in Optimizely CMS 12

Take control over culture-specific operations like date and time formatting.

Tomas Hensrud Gulla | Nov 15, 2024 | Syndicated blog

I'm running Optimizely CMS on .NET 9!

It works 🎉

Tomas Hensrud Gulla | Nov 12, 2024 | Syndicated blog

Recraft's image generation with AI-Assistant for Optimizely

Recraft V3 model is outperforming all other models in the image generation space and we are happy to share: Recraft's new model is now available fo...

Luc Gosso (MVP) | Nov 8, 2024 | Syndicated blog