Copied to clipboard

Flag this post as spam?

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


  • Luke 110 posts 256 karma points
    Nov 26, 2015 @ 15:08
    Luke
    0

    IPublishedContent Object Values

    Hi All,

    If I have an object of IPublishedContent how should I get the property values?

    Both of these work and run: Given: IPublishedContent ipc = Umbraco.Content(id);

    ipc.GetProperty("alias").Value
    ipc.GetPropertyValue<String>("alias")
    

    However the bottom one shows as an error in the view with Visual Studio - but I am sure the Umbraco documentation uses this one.

    Version is 7.2.* and 7.3.*

    Best Regards, L

  • Carl Jackson 139 posts 478 karma points
    Nov 26, 2015 @ 15:37
    Carl Jackson
    1

    Both work fine.

    The first would be more prone to error as GetProperty Could return null. Which would give you a null reference exception accessing Value.

    Get propertyValue returning null would just not output anything in a lot of cases (all?).

    the error is becasue of the typing

        @{var alias = ipc.GetPropertyValue<String>("alias");}
        @alias
    

    Will work, as will

    @(ipc.GetPropertyValue<string>("alias"))
    

    and

    @ipc.GetPropertyValue("alias")
    

    Which defaults to a string value

    Thanks

    Carl

  • Luke 110 posts 256 karma points
    Nov 26, 2015 @ 15:44
    Luke
    0

    Thanks Carl,

    I should have said this is showing as an error insisting that GetPropertyValue is not a method (or no definition) of IPublishedContent.

    Regards, L

  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    Nov 26, 2015 @ 15:48
    Lee Kelleher
    0

    Hi Luke,

    Just follow up on Carl's reply - which is totally right.

    The issue with the syntax of ipc.GetPropertyValue<string>("alias") within Visual Studio is that the Razor parser believes the opening angle-bracket to be HTML markup.

    By wrapping the whole expression in curly-brackets, that tells Razor to parse as C#/code.

    @(ipc.GetPropertyValue<string>("alias"))
    

    ...or you can use a code-block to assign to a variable and render that out.

    @{
        var value = Model.Content.GetPropertyValue<string>("alias")
    }
    <h1>@value</h1>
    

    Cheers,
    - Lee

  • Luke 110 posts 256 karma points
    Nov 26, 2015 @ 15:56
    Luke
    0

    Hi Guys,

    My code is in a block. I was just giving the most important part.

    The issue is that the method is not recognised by the intelisense.

    The compiler is complaining that IPUblishedContent does not contain the definition/method. But it is running correctly.

    so to clarify:

    Running and No Error Message

    @{
    ipc.GetProperty("alias").Value
    }
    

    Running But Showing Up As An Error

    @{
    ipc.GetPropertyValue<String>("alias")
    }
    

    I want to know which is officially the correct way to call the properties of IPublishedContent and why it would one would show as an error but run correctly??

    Regards, L

  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    Nov 26, 2015 @ 16:14
    Lee Kelleher
    2

    GetPropertyValue is an extension method within the "Umbraco.Web" namespace... so if it can't find it, then you may need to add it to the top of your partial...

    @using Umbraco.Web
    @inherits UmbracoTemplatePage
    

    If that doesn't work, I'm not sure why it wouldn't be picking it up :-(

  • Carl Jackson 139 posts 478 karma points
    Nov 26, 2015 @ 16:10
    Carl Jackson
    0

    I don't know about the error, sorry.

    However.

    GetPropertyValue("alias") is a shorter neat way of accessing the data in GetProperty("alias").Value

    GetProperty("alias"). allows you to get more than just the value, the property type from the CMS for example.

    So they both have uses depending on what you are doing.

    Thanks

  • Luke 110 posts 256 karma points
    Nov 26, 2015 @ 16:28
    Luke
    0

    Hi Guys,

    Thank you for your help. Lee your reply gave me clue. Maybe because i am using a custom page model it was not working. I have paced the Umbraco.Web at the top of the view page and now is all well.

    @using Umbraco.Web;
    @inherits Umbraco.Web.Mvc.UmbracoViewPage<MyPageModel>
    

    Cheers gents.

    Regards, L

Please Sign in or register to post replies

Write your reply to:

Draft