Copied to clipboard

Flag this post as spam?

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


  • Ryan 24 posts 106 karma points
    Apr 23, 2016 @ 13:06
    Ryan
    0

    Node GetPropertyValue returning null

    Hey guys, I don't know if it's just late here or what but I just can't for the life of me see what's going wrong here. I've got a LINQ statement that I'm trying to execute, but a certain custom property ("client")'s value of the nodes are always returning null. I expect JSON object or better yet serialize to my strongly typed model. To debug I extracted a method, as below:

    debug window

    As you can see I can now examine in more detail where the problem is... the GetPropertyValue

    What might I be missing here? Why can I see it in locals but the function returns null. I have used the function in views with similar custom properties without issue.

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Apr 23, 2016 @ 18:49
    Dan Diplo
    0

    Have you got a Property Value Converter that will convert from a JSON string to a JObject? Because Umbraco won't magically do the conversion for you. So if you try and get a strongly-typed model back from an instance of IPublishedContent for a type that hasn't got a converter you will get NULL back.

    I'd guess you might need to get the value back as a string and then manually convert it to a JObject.

  • Ryan 24 posts 106 karma points
    Apr 23, 2016 @ 22:03
    Ryan
    0

    No I don't... the documentation for GetPropertyDataValue() didn't mention that. It says that at the minimum JObject conversion would definitely be supported for JSON string values, so I just assumed that conversion would work.

    I don't understand all that stuff in the link you sent. It seems too complex for what I want to do, which is just get the client.id so I used Newtonsoft.Json to deserialize the

        private int GetClientId(Umbraco.Core.Models.IPublishedContent node)
        {
            var prop = node.GetPropertyValue<string>("client");
            if (prop == null) return -1;
            var jsonProp = JsonConvert.DeserializeObject<JObject>(prop);
            return jsonProp["id"].Value<int>();
        }
    
Please Sign in or register to post replies

Write your reply to:

Draft