Copied to clipboard

Flag this post as spam?

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


  • Gary Stenstrom 4 posts 66 karma points
    Apr 24, 2015 @ 04:21
    Gary Stenstrom
    0

    Retrieve custom property values from a content picker item

    I have added a content picker to a document type. I would like to access the custom properties of the content item in the template but am having problems doing that. 

    I can get the ID of the content Item ... 

    @var id = CurrentPage.companyContentItem;
    @var item = Umbraco.TypedContent(id);

    When I get the "TypedContent" for the Id a "Umbraco.Web.PublishedCache.XmlPublishedCache.XmlPublishedContent" is returned. 

    I appear to be able to get the default properties from this type ... 

    @var name = item.Name  //works

    ... but trying to get a custom property does not return anything. 

    @var customProperty = item.customProperty  //does not work

     

    How am I able to access the custom properties of a content item added to the content model via a "Content Picker"?

     

    Thanks,

    Gary

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Apr 24, 2015 @ 05:25
    Jan Skovgaard
    101

    Hi Gary

    I think the issue is you're mixing the syntax for strongly typed and dynamic razor. When using "CurrentPage" instead of "Model" you're using the dynamic version. Therefore you need to use Umbraco.Content(id) - Then you should be able to fetch the custom property like you're trying above. So the full could should look like this

    @var id = CurrentPage.companyContentItem;
    @var item = Umbraco.Content(id);
    
    @var customProperty = item.customProperty
    

    You might be able to benefit from using the "Razor cheatsheet" here https://our.umbraco.org/projects/developer-tools/umbraco-v6-mvc-razor-cheatsheets - It shows what one should use when doing either "Strongly typed" or "Dynamic". The first page displays the syntax for "Dynamics".

    Hope this helps and makes sense.

    EDITED: Corrected typo in code-sample (Stupid Copy/Paste!) :)

    /Jan

  • Dave Woestenborghs 3504 posts 12134 karma points MVP 9x admin c-trib
    Apr 24, 2015 @ 08:18
    Dave Woestenborghs
    2

    If you are using TypedContent you will get a strongly type IPublishedContent object.

    You can retreive the property like this with that:

    var customProperty = item.GetPropertyValue("customProperty") // you need to pass the correct alias of your property

    Dave

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Apr 24, 2015 @ 08:27
    Dennis Aaen
    1

    Hi Gary and Jan,

    I just like add something to the great post Jan already has written. First of all I think that Jan missed to change TypedContent to Content in his example. And you should not have the @ in front of you variable. So the full could should look like this:

    var id = CurrentPage.companyContentItem;
    var item = Umbraco.Content(id);

    var customProperty = item.customProperty

    @customProperty

    When this is said, you can use the .HasValue method to check if the content picker has a value picked on the current page, and only display the content if this is the case. As Jan alredy has pointed out is that there are a syntax for strongly typed and dynamic Razor. Here is some documentation about the built-in Umbraco v7+ property editors https://our.umbraco.org/documentation/using-umbraco/backoffice-overview/property-editors/Built-in-Property-Editors-v7/ If you are taking your case, where you are using the content picker https://our.umbraco.org/documentation/using-umbraco/backoffice-overview/property-editors/Built-in-Property-Editors-v7/Content-Picker

    As you can see there are two MVC View Exampleexamples on how to get data from the picker, and it´s refers to the strongly typed syntaxt and the dynamics, as Jan has pointed out.

    So your exmple with the check if the picker has a value on the current page, would look like this.

    var id = CurrentPage.companyContentItem;

    if (CurrentPage.HasValue("id")){
        var item = Umbraco.Content(id);
        <a href="@item.Url">@item.Name</a>
    }

    Hope this helps,

    /Dennis

  • Gary Stenstrom 4 posts 66 karma points
    Apr 24, 2015 @ 17:28
    Gary Stenstrom
    0

    I am actually embarrassed to ask ... when/how do I know when dynamic versus strongy-typed syntax should be/is being used? 

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Apr 24, 2015 @ 22:34
    Jan Skovgaard
    0

    Hi Gary

    Don't be :) - It is confusing to figure out why where and how sometimes. But in short it's just two different ways of achieving the same thing. There are of course some differences and most hardcore c# developers in the community seem to prefer using strongly typed because it gives them more control of things where the dynamic approach has some limitations I think... (Somebody correct me if I'm wrong please :)).

    But apart from that it's up to you to decide whether you're going to use the one or the other approach. I comes down the syntax you prefer writing I guess.

    The dynamic approach is more concise than the strongly typed model approach - I think it might be worth running through Jeavons slides from the german umbraco festival earlier this year where he talks about Razor and the different syntaxes http://www.slideshare.net/JeavonLeopold/umbraco-oktoberfest-2014

    Hope this helps a bit.

    /Jan

Please Sign in or register to post replies

Write your reply to:

Draft