Copied to clipboard

Flag this post as spam?

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


  • Marc Love (uSkinned.net) 447 posts 1790 karma points
    May 19, 2022 @ 11:51
    Marc Love (uSkinned.net)
    0

    ContentSavingNotification - Cannot change name of node before save

    Hi Folks,

    I am trying to hook into the ContentSavingNotification and change the name of a node being saved based on some conditions.

    It appears that you cannot change the name of a node.

    public void Handle(ContentSavingNotification notification)
    {
         foreach (IContent node in notification.SavedEntities)
         {
              if(SOME CONDITION IS MET)
              {
                  node.Name = "New Name";
              }
         }
    }
    

    If you inspect notification.SavedEntities the correct node's name is indeed changed however when the node appears in the backoffice it has the original name.

    This approach in Umbraco 8 using ContentService.Saving event worked however its not working in Umbraco 9 with the new approach.

    Anyone any ideas if this is an issue with Umbraco or I am doing something wrong?

    Cheers,

    Marc

  • Paul Seal 524 posts 2890 karma points MVP 7x c-trib
    May 19, 2022 @ 13:35
    Paul Seal
    0

    Hi Marc

    I seem to recall someone telling me to use this before:

    ContentService.CommitDocumentChanges(node);
    

    Give that a try.

    Here is a link to it being used in a test in the Umbraco repo. https://github.com/umbraco/Umbraco-CMS/blob/0134199035cf4caf0deddc24648be5cdeb27d2f9/tests/Umbraco.Tests.Integration/Umbraco.Infrastructure/Services/ContentServiceTests.cs#L1235

    Paul

  • Marc Love (uSkinned.net) 447 posts 1790 karma points
    May 19, 2022 @ 14:25
    Marc Love (uSkinned.net)
    0

    Hi Paul,

    Injected contentService into the constructor and not getting "CommitDocumentChanges" as an available method.

    Any ideas?

    Cheers,

    Marc

  • Marc Love (uSkinned.net) 447 posts 1790 karma points
    May 20, 2022 @ 09:04
    Marc Love (uSkinned.net)
    0

    Just checked and unfortunately CommitDocumentChanges is an internal method so cant access it. :(

  • Didier Marin 18 posts 90 karma points
    Jan 24, 2023 @ 14:10
    Didier Marin
    0

    Hi,

    you need to use SetCultureName for ex :

        foreach (var node in notification.SavedEntities)
        {
            foreach (var lg in node.AvailableCultures)
            {
                node.SetCultureName("new name", lg);
            }
        }
    
  • Laurent Lequenne 123 posts 248 karma points
    Aug 15, 2024 @ 16:30
    Laurent Lequenne
    0

    Merci gamin :-)

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies