Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

PropertyListBase with page reference import throws exception

Vote:
 

Hi guys,

We have a PropertyListBase 

[PropertyDefinitionTypePlugIn]
    public class PagesListProperty : PropertyListBase<ApplicationPage>
    {
    }

and Application page as 

 public class ApplicationPage
    {
        public PageReference Page { get; set; }

        public string Name { get; set; }
    }

 

This property works fine and we get a nice interface in edit mode, where we can enter multiple items and each item has a page reference and a string.

We have exported this page and trying to import in a new env, but getting the following error Unable to cast object of type 'EPiServer.Core.UnresolvedContentReference' to type 'EPiServer.Core.PageReference'.] & the import fails.

All the pages that are selected within this property are part of the export package. 

Anyone know how can I make this work?

Thanks

#263156
Edited, Sep 15, 2021 9:55
Vote:
 

I think ApplicationPage is missing inherited Episerver type, tyically ContentData

#263276
Sep 17, 2021 6:18
Vote:
 

I've used ContentReferences on PropertyLists and I've been able to import and export successfully between environments.  I have noted that where Url properties are used, these have needed decorating with the [JsonConverter(typeof(UrlConverter))] attribute if you want to use them in a property list or if you want to use it on a content type that will be imported and exported between systems.  I believe this is because the import / export mechanism uses json files.

This may actually be failing because there is a missing json convertor for PageReference, you could try writing your own simple convertor for this?

#264281
Oct 01, 2021 15:39
Vote:
 

Absolutely Stotty. We can add a PageReference converter like the one for UrlConverter. 

[ServiceConfiguration(typeof(JsonConverter))]
    public class PageReferenceConverter : JsonConverter
    {
        public override bool CanConvert(Type objectType) => typeof(PageReference).IsAssignableFrom(objectType);

        public override object ReadJson(
            JsonReader reader,
            Type objectType,
            object existingValue,
            JsonSerializer serializer)
        {
            return reader.Value != null ? (object)new PageReference(reader.Value.ToString()) : (object)null;
        }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            if (!(value is PageReference pageReference) || PageReference.IsNullOrEmpty(pageReference))
                writer.WriteValue(string.Empty);
            else
                writer.WriteValue(value.ToString());
        }
    }

~ Sujit

#264691
Edited, Oct 07, 2021 16:45
Vote:
 

When PropertyList is saved/loaded from db it does not use the converters registered in IOC container since those are used when sending/getting data from the client and that format is not nessecarily the same format as we want to persist in database. So to get you converter to be used when loading/persisting data from database you need to decorate your PageReference property on your model, like:

[JsonConverter(typeof(PageReferenceConverter))]
public PageReference Page { get; set; }
#265121
Oct 15, 2021 7:21
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.