0
votes

I'm attempting to replicate Umbraco's (Umbraco v7) process of updating a node name when there is an existing node with the same name.

But in this case, I'm building an archive process which will copy the node from Umbraco database into an archived database. The issue I'm facing is trying to make the URL's unique as I'll need to do the look-up against my archive to ensure the node name is unique.

Currently I'm firing a custom save method when the Umbraco save event is triggered which looks up the name against the archive and checks if the URL is unique

    protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
    {
        ContentService.Saving += ContentService_Saving;
    }

    void ContentService_Saving(IContentService sender, Umbraco.Core.Events.SaveEventArgs<IContent> e)
    {

        // create the content global ID
        foreach (var entity in e.SavedEntities)
        {
            var url = Helper.GetNiceUrlPath(entity);
            // fetch all items from the database with that name + url
            var result = AppArchiver.GetContentByUrl(Helper.CreateUrlFromName(entity.Name)).ToList();                

            if(result.Any())
            {
                // do something here with the name if possible?
            }

            if (!entity.HasAppId())
            {
                entity.SetAppId();
            }
        }
    }

I can't set the Name as its read only property... I can't set it using entity.SetValue("name", "blah (1)")

So what's my best bet to do this? Any suggestions? Because I'm banging my head against the wall searching through the Umbraco source code trying to work out how they do it.

The reason for storing these nodes in a database is because around 300 new nodes will be created a month and that will be unbearable to navigate them in Umbraco.

UPDATE: OK so it appears Umbraco does the URL unique check in the angular code before the item is posted back to the backend for saving. I'll have to create a service that does this if possible.

2

2 Answers

1
votes

I might have misunderstood the question, but instead of trying to copy the process, why not use the unique url already created by Umbraco?

I have something similar set up in our Umbraco installation triggering on Document.BeforeSave event. If I try running the following code:

private void Document_BeforeSave(Document sender, global::umbraco.cms.businesslogic.PublishEventArgs e)
{
    var url = umbraco.library.NiceUrl(sender.Id);
}

Then the url is already unique, as Umbraco has made sure to rename the Node before it's saved.

0
votes

ok so it looks like its not possible via the backend of umbraco. i'd have to write a hook into umbraco frontend angular code which looks up my archive via a service and returns if the url/name is unique.

looking at the http post umbraco does when you save/publish a document it has already appended the node name with a unique identifier (i.e. (1))

if/when i get round to writing that process i'll update this question.