Calling all developers! We invite you to provide your input on Feature Experimentation by completing this brief survey.

 

Alexander Haneng
Jan 23, 2013
  4684
(1 votes)

Getting EPiServer 6 custom properties to work in EPiServer 7: Cannot create and populate list type…

When working on getting EPiImage to work in EPiServer 7 I ran into the following error when publishing or auto saving an EPiImageGalleryProperty.

 

clip_image002

 

From the error message I guessed it had something to do with the JSON communication used in the new UI.

 

Turns out that since EPiImageGalleryProperty stores its data as an EPiImageGalleryImageCollection object, EPiServer 7 doesn’t know how to serialize it as a JSON object. But how can we tell EPiServer explicitly how to do the serialization?

 

Luckily Linus Ekström (EPiServer Sweden) helped me out with some sample code:

using System;
using EPiServer.ServiceLocation;
using Newtonsoft.Json;
 
namespace EPiServer.Cms.Shell.Json
{
    /// <summary>
    /// Json converter thas handles conversion between 
    /// <see cref="Url"/> and <see cref="string"/>.
    /// </summary>
    [ServiceConfiguration(typeof(JsonConverter))]
    public class UrlConverter : JsonConverter
    {
        public override bool CanConvert(Type objectType)
        {
            return typeof(Url).IsAssignableFrom(objectType);
        }
 
        public override object ReadJson(
            JsonReader reader, 
            Type objectType, 
            object existingValue, 
            JsonSerializer serializer)
        {
            return (reader.Value == null) ? null : 
                new Url(reader.Value.ToString());
        }
 
        public override void WriteJson(
            JsonWriter writer, 
            object value, 
            JsonSerializer serializer)
        {
            var url = value as Url;
            if (url == null || url.IsEmpty())
            {
                writer.WriteValue(string.Empty);
                return;
            }
            writer.WriteValue(value.ToString());
        }
    }
}

 

This class which inherits from JsonConverter tells EPiServer 7 how to do the conversion of a EPiServer.Url to Json (meaning a string representation) and back again.

 

I can create my own Json converter for EPiImageGalleryImageCollection like this:

using System;
using EPiServer.ServiceLocation;
using Newtonsoft.Json;
 
namespace MakingWaves.EPiImage.EPiImage.Code
{
    /// <summary>
    /// Json converter that handles conversion between 
    /// EPiImageGalleryImageCollectionConverter 
    /// and string for the new EPiServer 7 UI
    /// </summary>
    [ServiceConfiguration(typeof(JsonConverter))]
    public class EPiImageGalleryImageCollectionJsonConverter : JsonConverter
    {
        public override bool CanConvert(Type objectType)
        {
            return typeof(EPiImageGalleryImageCollection).IsAssignableFrom(objectType);
        }
 
        public override object ReadJson(
            JsonReader reader, 
            Type objectType, 
            object existingValue, 
            JsonSerializer serializer)
        {
            if (reader.Value == null)
                return null;
 
            return new EPiImageGalleryImageCollection(reader.Value.ToString());
        }
 
        public override void WriteJson(
            JsonWriter writer, 
            object value, 
            JsonSerializer serializer)
        {
            EPiImageGalleryImageCollection gallery = value 
                as EPiImageGalleryImageCollection;
            if (gallery == null || gallery.Count == 0)
            {
                writer.WriteValue(string.Empty);
                return;
            }
            writer.WriteValue(gallery.ToString());
        }
 
    }
}

 

For it to work I also needed to do the following changes to my existing EPiImage code:

1. Create a ToString method for EPiImageGalleryImageCollection that accurately serialized the data in the object to a string. As it consists of a List<EPiImageGalleryImage> I also need to:

2. Create a ToString method for EPiImageGalleryImage that accurately serialized the data in the object to a string

3. Create a constructor method for EPiImageGalleryImageCollection that could take in the serialized string as a parameter to recreate the object

4. Create a constructor method for EPiImageGalleryImage that could take in the serialized string as a parameter to recreate the object

 

If you are converting your own custom property to work with EPiServer 7, I thought this code might guide you in the right direction Smile

Jan 23, 2013

Comments

Please login to comment.
Latest blogs
Optimizely Content Graph on mobile application

CG everywhere! I pull schema from our default index https://cg.optimizely.com/app/graphiql?auth=eBrGunULiC5TziTCtiOLEmov2LijBf30obh0KmhcBlyTktGZ in...

Cuong Nguyen Dinh | Jan 20, 2025

Image Analyzer with AI Assistant for Optimizely

The Smart Image Analyzer is a new feature in the Epicweb AI Assistant for Optimizely CMS that automates the management of image metadata, such as...

Luc Gosso (MVP) | Jan 16, 2025 | Syndicated blog

How to: create Decimal metafield with custom precision

If you are using catalog system, the way of creating metafields are easy – in fact, you can forget about “metafields”, all you should be using is t...

Quan Mai | Jan 16, 2025 | Syndicated blog

Level Up with Optimizely's Newly Relaunched Certifications!

We're thrilled to announce the relaunch of our Optimizely Certifications—designed to help partners, customers, and developers redefine what it mean...

Satata Satez | Jan 14, 2025

Introducing AI Assistance for DBLocalizationProvider

The LocalizationProvider for Optimizely has long been a powerful tool for enhancing the localization capabilities of Optimizely CMS. Designed to ma...

Luc Gosso (MVP) | Jan 14, 2025 | Syndicated blog

Order tabs with drag and drop - Blazor

I have started to play around a little with Blazor and the best way to learn is to reimplement some old stuff for CMS12. So I took a look at my old...

Per Nergård (MVP) | Jan 14, 2025