Prevent content events from the loop
I'm writing a simpler solution to the problem that most of us keep getting while working with ContentEvents. There might be solution to this already but at least I couldn't find it easily.
I was working on content events where on PublishedContent I had to refer products under the category. Now this needs ContentEvents
_contentEvents.PublishedContent += ContentEvents_PublishedContent;
private static void ContentEvents_PublishedContent(object sender, ContentEventArgs e)
{
SaveDynamicCategory(e.Content);
}
The method above SaveDynamicCategory has a save method as below -
ContentRepository.Save(content, SaveAction.Publish, AccessLevel.NoAccess);
This works perfectly but the problem that comes with it that it keeps on calling PublishedContent deligate. Therfore the solution is to use piped SaveAction along with SkipValidation as below -
ContentRepository.Save(content, SaveAction.Publish | SaveAction.SkipValidation, AccessLevel.NoAccess);
And if publish method check this enum containing SkipValidation - if true then stop the loop and return it -
private static void ContentEvents_PublishedContent(object sender, ContentEventArgs e)
{
if (e is SaveContentEventArgs saveContentEventArgs && saveContentEventArgs.Action.HasFlag(SaveAction.SkipValidation))
{
return;
}
SaveDynamicCategory(e.Content);
}
Hope it helps. Thank you for your time.
Comments