U4-4147 - SaveAnPublishWithStatus always triggers save event

Created by florian verdonck 27 Jan 2014, 15:18:50 Updated by Thomas Beckert 11 Mar 2016, 17:20:06

Relates to: U4-4748

Take the following code:

public class EventsHandler : ApplicationEventHandler { protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) { ContentService.Published += ContentService_Published;

        base.ApplicationStarted(umbracoApplication, applicationContext);
    }

    void ContentService_Published(Umbraco.Core.Publishing.IPublishingStrategy sender, Umbraco.Core.Events.PublishEventArgs<Umbraco.Core.Models.IContent> e)
    {
        foreach (var item in e.PublishedEntities)
        {
            if(item.ContentType.Alias == "Page")
            {
                string currentAlias = item.GetValue<string>("umbracoUrlAlias");
                string newAlias = ("my-page-" + item.Name).ToLower().Trim();

                if(currentAlias != newAlias)
                {
                item.SetValue("umbracoUrlAlias",newAlias );
                Umbraco.Core.ApplicationContext
                    .Current.Services.ContentService.SaveAndPublishWithStatus(item, 0, false);
                }
            }
        }

    }
}

The code .SaveAndPublishWithStatus triggers the save event even when the last parameter is false. I suspect that this is a bug. I included a workaround in my example but I though I let someone know.

Comments

Sebastiaan Janssen 27 Jan 2014, 15:56:30

I can repro, it's the Publish event that gets triggered again (at least: breakpoints don't hit for the Saved / Saving event). Also goes for the SaveAndPublish method.


Shannon Deminick 24 Apr 2014, 05:22:14

This is unavoidable. If the published event is not raised then the xml cache will not be updated nor will Examine or any other listeners on publishing. The raiseEvents flag on that method will supress the Save events but not the published, but as mentioned this cannot be avoided otherwise you will end up with inconsistent data in caches.


Thomas Beckert 11 Mar 2016, 17:20:06

What if you send a flag, where you can see in the save function, if this node is also going to be published. For me this would work so I can do things in the save function and call save and publish if I know, that this node would be published anyway.


Priority: Minor

Type: Bug

State: Closed

Assignee: Shannon Deminick

Difficulty: Normal

Category: Extensibility

Backwards Compatible: True

Fix Submitted:

Affected versions: 6.1.6, 7.0.1, 7.0.2

Due in version:

Sprint:

Story Points:

Cycle: