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

Custom Virtual Property

Vote:
 

Hello,

I'm wondering if its possible to add "virtual" property to model with its metadata.

Idea of it was to have a property inside Editor page where I can type some value which will be stored outside the tblBigTable (it can be even an external API).

I found that metadata for model can be created by IMetadataProvider.

using TestProject.Models.Pages;
using EPiServer.Core;
using EPiServer.Shell.ObjectEditing;
using System;
using System.Collections.Generic;
using System.Linq;

namespace TestProject.Metadata
{
    public class CustomMetadataProvider : IMetadataProvider
    {
        private IMetadataProvider _metadataProvider;

        public CustomMetadataProvider(IMetadataProvider metadataProvider)
        {
            _metadataProvider = metadataProvider;
        }

        public ExtendedMetadata CreateMetadata(IEnumerable attributes, Type containerType, Func modelAccessor, Type modelType, string propertyName)
        {
            return _metadataProvider.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName);
        }

        public IEnumerable GetMetadataForProperties(object container, Type containerType)
        {
            IList result = _metadataProvider.GetMetadataForProperties(container, containerType).ToList();

            if (container is ArticlePage)
            {
                ExtendedMetadata virtualFieldMetadata = new ExtendedMetadata(containerType, null, typeof(PropertyLongString), "MyVirtualField")
                {
                    DisplayName = "Virtual Field"
                };
                
                result.Add(virtualFieldMetadata);
            }

            return result.ToArray();
        }
    }
}

and initalizable module to register it:

using TestProject.Models.Pages;
using EPiServer.Cms.Shell;
using EPiServer.Core;
using EPiServer.DataAbstraction.RuntimeModel;
using EPiServer.Framework;
using EPiServer.Framework.Initialization;
using EPiServer.ServiceLocation;
using EPiServer.Shell.ObjectEditing;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web.ModelBinding;

namespace TestProject.Metadata
{
    [InitializableModule]
    [ModuleDependency(typeof(InitializableModule))]
    public class MetadataInitializableModule : IInitializableModule
    {
        public void Initialize(InitializationEngine context)
        {
            MetadataHandlerRegistry handlerRegistry = ServiceLocator.Current.GetInstance();

            IEnumerable metadataHandlers = handlerRegistry.GetMetadataHandlers(typeof(ContentData));
            IMetadataProvider metadataProvider = (IMetadataProvider)metadataHandlers.First(i => i is IMetadataProvider);
            IMetadataProvider customMetadataProvider = new CustomMetadataProvider(metadataProvider);

            handlerRegistry.RegisterMetadataHandler(typeof(ContentData), customMetadataProvider );
            
        }

        public void Preload(string[] parameters) { }

        public void Uninitialize(InitializationEngine context) { }
    }
}

after this im getting field without any input to put value and when I'm trying to add ClientEditingClass to my custom dojo widget - page stops to display with message that metadataProvider can not be resolved.

There is no official documentation how to use the API and how to create ExtendedMetadata properly. Is it possible to get information how it should be used?

#179045
May 30, 2017 11:43
Vote:
 

Message which shows on error looks like this:

"Unable to load /EPiServer/shell/Stores/metadata/EPiServer.Core.ContentData?modelAccessor=%7B%22contentLink%22%3A%22107356_565607%22%7D&dojo.preventCache=1496137912462 status: 500"
#179048
May 30, 2017 11:54
* 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.