Serialize EPiServer IContent
using System.Dynamic; | |
using EPiServer.Core; | |
namespace PixieDigital.EpiServer.Extensions.Contents | |
{ | |
public interface ISerializeContents | |
{ | |
ExpandoObject SerializeableContent(IContent content); | |
ExpandoObject SerializeableContent(ContentReference contentReference); | |
} | |
} |
using System; | |
using System.Collections.Generic; | |
using System.Dynamic; | |
using System.Linq; | |
using EPiServer; | |
using EPiServer.Core; | |
namespace PixieDigital.EpiServer.Extensions.Contents | |
{ | |
public class SerializeContents : ISerializeContents | |
{ | |
private readonly IContentLoader _contentLoader; | |
public SerializeContents(IContentLoader contentLoader) | |
{ | |
_contentLoader = contentLoader; | |
} | |
public ExpandoObject SerializeableContent(IContent content) | |
{ | |
string mediaInstantceName = "MediaData"; | |
dynamic expandoObject = IntializeExpandoObject(content); | |
var properteis = expandoObject as IDictionary<string, object>; | |
if (properteis == null) return expandoObject; | |
// Media types | |
var data = content as MediaData; | |
if (data != null) | |
{ | |
properteis.Add(mediaInstantceName, IntializeMedia(data)); | |
} | |
else if (content != null) | |
{ | |
//Properties | |
foreach (var propertyData in content.Property) | |
{ | |
if (propertyData.Value == null) continue; | |
if (propertyData.Value is Boolean || propertyData.Value is Int32 || propertyData.Value is DateTime || propertyData.Value is Double) | |
{ | |
properteis.Add(propertyData.Name, propertyData.Value); | |
} | |
else if (propertyData.Value is string[]) | |
{ | |
properteis.Add(propertyData.Name, ((string[])propertyData.Value)); | |
} | |
else if (propertyData.Type == PropertyDataType.Block && propertyData.Value != null) | |
{ | |
//Iterate for Block Properties | |
properteis.Add(propertyData.Name, SerializeableContent(propertyData.Value as IContent)); | |
} | |
else if (propertyData is EPiServer.SpecializedProperties.PropertyContentArea) | |
{ | |
//Iterate | |
var contentAreaItems = PropertyContentObjects(propertyData as EPiServer.SpecializedProperties.PropertyContentArea); | |
properteis.Add(propertyData.Name, contentAreaItems.ToArray()); | |
} | |
else | |
{ | |
properteis.Add(propertyData.Name, (propertyData.Value != null) ? propertyData.ToWebString() : null); | |
} | |
} | |
} | |
return expandoObject; | |
} | |
public ExpandoObject SerializeableContent(ContentReference contentReference) | |
{ | |
var content = _contentLoader.Get<IContent>(contentReference); | |
return SerializeableContent(content); | |
} | |
private dynamic IntializeExpandoObject(IContent c) | |
{ | |
if (c is null) return new ExpandoObject(); | |
dynamic content = new ExpandoObject(); | |
content.ContentLink = c.ContentLink; | |
content.ContentTypeID = c.ContentTypeID; | |
content.Name = c.Name; | |
content.ParentLink = c.ParentLink; | |
content.ContentGuid = c.ContentGuid; | |
return content; | |
} | |
private dynamic IntializeMedia(MediaData media) | |
{ | |
dynamic mediaData = new ExpandoObject(); | |
mediaData.MimeType = media.MimeType; | |
mediaData.RouteSegment = media.RouteSegment; | |
return mediaData; | |
} | |
private IEnumerable<ExpandoObject> PropertyContentObjects(EPiServer.SpecializedProperties.PropertyContentArea propertyContentArea) | |
{ | |
var contentArea = propertyContentArea.Value as ContentArea; | |
var contentAreaItems = new List<ExpandoObject>(); | |
if (contentArea != null) | |
{ | |
contentAreaItems.AddRange(contentArea.Items.Select(itm => SerializeableContent(itm.GetContent())).Select(itmobj => itmobj).Cast<dynamic>().Cast<ExpandoObject>()); | |
} | |
return contentAreaItems; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment