Copied to clipboard

Flag this post as spam?

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


  • Peter Gregory 408 posts 1614 karma points MVP 3x admin c-trib
    Dec 30, 2013 @ 09:36
    Peter Gregory
    0

    How to get the alias of the Property Editor type used from a published property?

    Hey all. Is there an easy way to get the editor type of a published property from the IPublishedContent?

    I need to iterate through the properties in the Properties collection and based on the type do different things. However I cant see how I can get the alias of the editor type for a property.

    EG lets say I wanted to do the following.

    @foreach(var prop in Model.Content.Properties){
    
        if(prop.PropertyType.PropertyEditorAlias == "Umbraco.MediaPicker"){
            //then do something here
        }
    
    }   
    

    However my problem is that even though I can see the PropertyType object on the IPublishedProperties base class it is internal and I cannot access it.

    Also before anyone says just use propertyTypeAlias that will not return what I want as that just returns the Property alias as defined on the document type. I need to go one step deeper and get the Editor type for the defined property.

  • Peter Gregory 408 posts 1614 karma points MVP 3x admin c-trib
    Dec 30, 2013 @ 09:59
    Peter Gregory
    104

    I have resolved my own issue. Heres how I did it in case someone else goes looking for a similar answer in the future (edge case but none the less)

    @{
        var contType = Model.Content.ContentType;
     }
    
    @foreach(var prop in Model.Content.Properties){
       var propType = contType.GetPropertyType(prop.PropertyTypeAlias).PropertyEditorAlias;
       if(propType  == "Umbraco.MediaPicker"){
           //then do something here
       } 
    
    } 
    
  • Morten Christensen 596 posts 2773 karma points admin hq c-trib
    Dec 30, 2013 @ 12:45
    Morten Christensen
    0

    Good that you find a way to do it ;-)  I thought I'd offer an alternative approach, as this seems like a scenario that could be handled by a simple naming convention. I assume that you want to check for the alias of the PropertyEditor because it can be used on many different property types, which are usually named differently. But if you are developing a site and not a package you could simply append something like "MediaPicker" to the alias of the PropertyTypes that use this specific PropertyEditor / DataType. And if you need to ensure this is done consistently by others you could create an event handler for ContentType.Saving that updates the alias for PropertyTypes that use this PropertyEditor. And then you could do this:

    @foreach(var prop inModel.Content.Properties){

       
    if(prop.PropertyTypeAlias.EndsWith("MediaPicker"){
           
    //then do something here
       
    }

    }

     

    Another approach could be to use a "Property Editor Value Converter" (http://our.umbraco.org/documentation/Extending-Umbraco/Property-Editors/value-converters-v6) for your MediaPicker, so you can use / check the type of a Property's value. Pseudocode example:

    if(prop.Value is TypedMediaPickerValue) {}


    Just something to think about,

    - Morten

  • julius 107 posts 289 karma points
    Jan 02, 2014 @ 22:17
    julius
    0

    I had to get to the property editor alias in backend code and used the content type service:

    IContentType contentType = contentTypeService.GetContentType(CurrentPage.DocumentTypeId);
    IEnumerable propertyTypes = contentType.CompositionPropertyTypes;

    foreach (var propertyType in propertyTypes)
    {
    string propertyEditorAlias = propertyType.PropertyEditorAlias;

    // Do stuff here based on the property editor alias.
    }
Please Sign in or register to post replies

Write your reply to:

Draft