Copied to clipboard

Flag this post as spam?

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


  • jake williamson 207 posts 873 karma points
    Nov 15, 2017 @ 09:40
    jake williamson
    0

    how do you get a nested content row unique 'key'?

    hey out there,

    i've just started a project where we're using nested content instead of archetype. everything is working a treat with ditto however, we've ran into a bit of a problem getting the unique identifier for the row... the build in processor doesn't appear to output it?

    we posted up a general question on the problem to see if it was possible outside of ditto and it appears it is using 'GetKey()':

    https://our.umbraco.org/forum/using-umbraco-and-getting-started/89149-is-it-possible-to-access-the-key-value-from-a-nested-content-item#comment-281778

    the only way we could figure out how to get the key is to decorate a guid property on our model with the following processor:

    public class NestedContentKeyAttribute : DittoProcessorAttribute
    {
        public override object ProcessValue()
        {
            if (string.IsNullOrEmpty(Value?.ToString()))
            {
                return null;
            }
    
            var key = ((IPublishedContent)Value).GetKey();
    
            return key;
        }
    }
    

    this is working, however i wanted to check that i'm not duplicating work and this functionality is already in ditto and we've somehow missed it?!

    any feedback would be great!

    cheers,

    jake

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Nov 15, 2017 @ 10:05
    Lee Kelleher
    0

    Hi Jake,

    Is this NestedContent in Umbraco core or standalone package? and which versions?

    Only as there are subtle differences with the guid key - which have been ported over to the package version, but if you have data saved with an older version it may not have the guid key


    I'm assuming that it's the Umbraco core version, so I would have expected this to work in your POCO...

    public Guid Key { get; set; }
    

    If that doesn't work, then I suspect there is some object-type unboxing happening in Ditto, as we do use IPublishedContent explicitly, as opposed to IPublishedContentWithKey.

    It'd be interesting to make this work out the box.

    Cheers,
    - Lee

  • jake williamson 207 posts 873 karma points
    Nov 15, 2017 @ 10:17
    jake williamson
    0

    hi lee,

    thanks for the feedback, much appreciated. we're using umbraco v7.7.4 and ditto v0.11.3 in the project and we're using the nested content datatype that ships with umbraco.

    interestingly, my starting point was to try:

    public Guid Key { get; set; }
    

    but i wasn't getting a value back...

    i'd read this post which suggested adding a 'key' value to the content, however the post was older and used the package rather than the version in umbraco...

    debugging the code and inspecting the property gave me:

    [{
        "key": "8b7e1484-834a-43e5-9707-d528cd83a984",
        "name": "item one",
        "ncContentTypeAlias": "myAlias",
        "title": "item one title",
        "price": "150.00"
    }]
    

    so i could def see that a guid was being returned with the key and that led me to try the 'NestedContentKeyAttribute' route.

    if i downloaded the ditto project from git and included it in the project so i can debug, is there a particular processor i should look at?

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Nov 15, 2017 @ 11:47
    Lee Kelleher
    0

    Hi Jake,

    The "key" property was added to NC whilst we were porting it over to the Umbraco core - I've since backported it to NC package version, but only applies to the latest release (v0.5.0).

    The code in Ditto that attempts to get the value from the object (class) itself is here... https://github.com/leekelleher/umbraco-ditto/blob/develop/src/Our.Umbraco.Ditto/ComponentModel/Processors/UmbracoPropertyAttribute.cs#L195

    Let me know how you get on with it.

    Thanks,
    - Lee

  • David Peck 690 posts 1896 karma points c-trib
    Nov 15, 2017 @ 11:57
    David Peck
    0

    I created a 'Guid' property editor, which basically created a hidden Guid the first time a piece of content is created, specifically for this purpose. It sounds like you have a working, better solution but I can share it if it helps anyone.

Please Sign in or register to post replies

Write your reply to:

Draft