Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jamesrpatterson
Created January 12, 2013 16:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesrpatterson/4519059 to your computer and use it in GitHub Desktop.
Save jamesrpatterson/4519059 to your computer and use it in GitHub Desktop.
Initialisation module for EPiServer which finds Dynamic Content instances within editable areas and adds any referenced image file to Mirroring exporter.
namespace MyWebsite.Initialization
{
using System;
using System.Collections.Generic;
using System.Linq;
using EPiServer;
using EPiServer.Core;
using EPiServer.DynamicContent;
using EPiServer.Enterprise;
using EPiServer.Enterprise.Enterprise;
using EPiServer.Framework;
using EPiServer.Framework.Initialization;
using EPiServer.SpecializedProperties;
using HtmlAgilityPack;
using log4net;
[InitializableModule]
[ModuleDependency(typeof(DynamicDataTransferHandler))]
public class MirroringDynamicContentImageUrl : IInitializableModule
{
private static readonly ILog logger = LogManager.GetLogger(typeof(MirroringDynamicContentImageUrl));
public void Initialize(InitializationEngine context)
{
DataExporter.PageExporting += new PageExportingEventHandler(DataExporter_PageExporting);
}
protected void DataExporter_PageExporting(DataExporter dataExporting, PageExportingEventArgs e)
{
var page = DataFactory.Instance.GetPage(e.PageReference);
// Search each page property for dynamic content instances.
foreach (PropertyData property in page.Property)
{
var xhtmlProperty = property as PropertyXhtmlString;
// We only want to go into properties which are PropertyXhtmlString.
if (xhtmlProperty != null && !xhtmlProperty.IsNull && (xhtmlProperty.Value as string) != null)
{
var body = new HtmlDocument();
body.LoadHtml(xhtmlProperty.Value as string);
// For all dynamic content controls, check all properties for image references.
foreach (IDynamicContent dynamicContent in this.GetDynamicContents(body))
{
foreach (PropertyData dcProperty in dynamicContent.Properties)
{
// If the property is an image property and has a value,
// add the value to the file transfer handler.
if (dcProperty is PropertyImageUrl && !dcProperty.IsNull)
dataExporting.FileTransfer.AddFile(dcProperty.Value as string);
}
}
}
}
}
private IList<IDynamicContent> GetDynamicContents(HtmlDocument htmlDoc)
{
IList<IDynamicContent> dynamicContentCollection = new List<IDynamicContent>();
// Get all html nodes which have a class of epi_dc.
IEnumerable<HtmlNode> dcNodes = htmlDoc.DocumentNode.Descendants().Where(n => n.Attributes["class"] != null && n.Attributes["class"].Value == "epi_dc");
foreach (HtmlNode item in dcNodes)
{
// Create an instance of the dynamic content and add to the collection for returning.
IDynamicContent dynamicContent = this.CreateDynamicContentFromNode(item);
if (dynamicContent != null)
dynamicContentCollection.Add(dynamicContent);
}
return dynamicContentCollection;
}
private IDynamicContent CreateDynamicContentFromNode(HtmlNode htmlNode)
{
// For some reason the state stored within the HTML for DCs created using the
// UserControl method doesn't work when passed into the CreateDynamicContent method.
// DCs created in this fashion, with Image URL properties, will not be read.
try
{
return DynamicContentFactory.Instance.CreateDynamicContent(htmlNode.Attributes["data-dynamicclass"].Value, htmlNode.Attributes["data-state"].Value, htmlNode.Attributes["data-hash"].Value, true);
}
catch (FormatException ex)
{
logger.Warn(ex.Message, ex);
return null;
}
}
public void Preload(string[] parameters)
{
}
public void Uninitialize(InitializationEngine context)
{
DataExporter.PageExporting -= new PageExportingEventHandler(DataExporter_PageExporting);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment