Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Tim Anderson 20 posts 83 karma points
    Jan 27, 2014 @ 15:13
    Tim Anderson
    0

    v6.1.6 IPropertyEditorValueConvertor, IContent and prevalues: error with inherited property on document type.

    Hi,

    I am currently working on an Umbraco 6 website using v6.1.6. I have sucessfully created a custom datatype, using various information on the forum / blogs (Tim Geyssens has been particularly insightful!) but I have stumbled across a problem I have not been able to figure out and was looking for some help from anyone.

    My custom datatype is querying an external database through a library - as such my datatype is storing a json array of data with ID's of records (objects in an external class library). I am then using an IPropertyEditorValueConverter to translate these ID values into a List of objects in a Macro Partial View so that I can access their properties for displaying out to the rendered page.

    Inside my IPropertyEditorValueConverter I used the code found in the following forum post to get the prevalues for my property - a connectionstring name used to connect to the external database - and all appeared to be working fine.

    However I have found that since moving the property to a base document type this stopped working and I now get a NullReferenceException when trying to get the prevalues from the Property.

    I.E. with the following structure.

    Document Types -- BasePage ---- HomePage

    When using the IPropertyEditorValueConverter on a template for the homepage, when my custom datatype is added as a property on the homepage, the convertor works fine, when the property is defined on the basepage and inherited by the homepage the converter fails and the exception is thrown.

    Here is the code I am working with

    The IPropertyEditorValueConvertor

    public class BoundariesPropertyEditorValueConvertor : IPropertyEditorValueConverter
    {
        public bool IsConverterFor(Guid propertyEditorId, string docTypeAlias, string propertyTypeAlias)
        {
            return Guid.Parse("EB55D501-901C-462E-871C-A8843B424DC3").Equals(propertyEditorId);
        }
    
        public Attempt<object> ConvertPropertyValue(object value)
        {
            if (UmbracoContext.Current != null)
            {
                var cs = UmbracoContext.Current.Application.Services.ContentService;
                var ds = UmbracoContext.Current.Application.Services.DataTypeService;
    
                IContent content = cs.GetById(UmbracoContext.Current.PageId.Value);
                PropertyType propertyType = content.ContentType.PropertyTypes.FirstOrDefault(x => Guid.Equals(x.DataTypeId, new Guid("EB55D501-901C-462E-871C-A8843B424DC3")));
                // Error is thrown on the line below because the line above does not see the inherited field in the property collection and returns a null object.
                IEnumerable<String> prevalues = ds.GetPreValuesByDataTypeId(propertyType.DataTypeDefinitionId);
    
                /* Other code here removed as not relevent to this issue */
    
                return new Attempt<object>(true, propertyValue);
            }
            else
            {
                return Attempt<object>.False;
            }
        }
    }
    

    A simplified version of my PartialView used by my macro.

    @using DCC.API.Boundaries.BE;
    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @{
    BoundarySelectedValues typedValue = Model.Content.GetPropertyValue<BoundarySelectedValues>("boundaries");
    if (typedValue != null)
    {
        <div>
            <h3>This content has been assigned to</h3>
            <p><strong>Areas:</strong>
            @foreach (BoundaryItem bi in typedValue.Items)
            {
                <span>@bi.Name ,</span>
            }
            </p>
        </div>
    }
    

    }

    Hopefully this all makes sense and any help would be greatly appreciated, I can get round this by defining the property for each document type, but I would rather use a base document type to avoid having to specify the field on every single page.

    I suppose what I am asking is, is there a correct way to retrieve properties for a piece of content in the Content API that have been inherited from a base type as I have not been able to figure this out yet.

    Kind regards,

    Tim

Please Sign in or register to post replies

Write your reply to:

Draft