Copied to clipboard

Flag this post as spam?

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


  • Heather Floyd 610 posts 1032 karma points MVP 6x c-trib
    Sep 12, 2019 @ 22:55
    Heather Floyd
    0

    How can I get the previous Node Name for a "dirty" IContent?

    I am trying to log node name changes in a property for future reference/usage.

    According to the documentation about IRememberBeingDirty, it is possible to access information about whether a property was changed.

    What I am trying to do is get the current node Name and the previous node Name to compare.

    When I debug this code snippet ('node' is an IContent entity):

    ...
    var dirty = (IRememberBeingDirty)node;
    var nameChanged = dirty.WasPropertyDirty("Name");
    ...
    

    "nameChanged" is false (though I did change the Node name for my test)

    If I hover over the "dirty" variable... I see properties "Name" = with the new name, and "NodeName" with the old name - but I can't figure out how to access "NodeName" via code.

    If I try this:

     var oldNodeName = ((Content)dirty).NodeName;
     var currentNodeName = node.Name;
    

    ".NodeName" had the red squiggle underline and says "Cannot access internal property 'NodeName' here"

    So... how can I get to that data?

  • Steve Megson 151 posts 1024 karma points MVP c-trib
    Sep 13, 2019 @ 10:00
    Steve Megson
    100

    IRememberBeingDirty only stores details of which properties were changed, not the previous values, so I don't think it can help you. Could you do your checks in the Saving event? At that stage, you can still get the previous version from the ContentService, since the updated version hasn't been saved yet.

    I use this code to populate umbracoUrlName with the original URL when a document is renamed:

    void Content_Saving(IContentService sender, SaveEventArgs<IContent> e)
    {
        foreach (var c in e.SavedEntities)
        {
            // Set umbracoUrlName if a published document is renamed and umbracoUrlName is empty
            if (c.HasIdentity 
                && c.HasPublishedVersion
                && c is ICanBeDirty
                && (c as ICanBeDirty).IsPropertyDirty("Name")
                && c.HasProperty("umbracoUrlName")
                && String.IsNullOrEmpty( c.GetValue<string>("umbracoUrlName"))
               )
            {
                var previousVersion = sender.GetById(c.Id);
                c.SetValue("umbracoUrlName", previousVersion.Name.ToUrlSegment());
            }
        }
    }
    

    NodeName seems to be left over from preparation for the language variant support that ended up being in version 8. It's intended to store the name in the default language when a node has language variants. It ends up storing the original name because it isn't being updated when you save content with a new name, but it's not intended to be a way to get the original name.

  • Heather Floyd 610 posts 1032 karma points MVP 6x c-trib
    Sep 14, 2019 @ 02:22
    Heather Floyd
    0

    Thanks so much, Steve!

    That is exactly what I needed.

    I was using an event attached to "ContentService.Saving" but wasn't aware of how to access the "old" content node values.

    I've got it working now :-)

Please Sign in or register to post replies

Write your reply to:

Draft