Try our conversational search powered by Generative AI!

Manoj Kumawat
Mar 8, 2022
  1514
(1 votes)

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.

Mar 08, 2022

Comments

Jake Jones
Jake Jones Mar 8, 2022 11:05 AM

Hi Manoj,

Thanks for sharing.

Why not just use PublishingContent? It occurs when content is requested to be published, so before the actual publish event, meaning you could modify the content before publishing and wouldn't need to re-publish it yourself.

If you do need to publish twice, would you be better off just checking the dynamic category value you're setting? The risk with your approach is that the SkipValidation flag now has a potentially unexpected consequence, which may be an issue if you end up using in elsewhere in the solution.

Anyway, just my thoughts 😉

Manoj Kumawat
Manoj Kumawat Mar 8, 2022 11:12 AM

Hello Jake, I expected that coming 😉 and thank you for pointing that out.

I had the bad example to share but this is not about publishing/published events. It is just about how you can use SaveContentEventArgs while you are stuck in loop at any event. I would like to correct that in blog too.

Please login to comment.
Latest blogs
Optimizely and the never-ending story of the missing globe!

I've worked with Optimizely CMS for 14 years, and there are two things I'm obsessed with: Link validation and the globe that keeps disappearing on...

Tomas Hensrud Gulla | Apr 18, 2024 | Syndicated blog

Visitor Groups Usage Report For Optimizely CMS 12

This add-on offers detailed information on how visitor groups are used and how effective they are within Optimizely CMS. Editors can monitor and...

Adnan Zameer | Apr 18, 2024 | Syndicated blog

Azure AI Language – Abstractive Summarisation in Optimizely CMS

In this article, I show how the abstraction summarisation feature provided by the Azure AI Language platform, can be used within Optimizely CMS to...

Anil Patel | Apr 18, 2024 | Syndicated blog

Fix your Search & Navigation (Find) indexing job, please

Once upon a time, a colleague asked me to look into a customer database with weird spikes in database log usage. (You might start to wonder why I a...

Quan Mai | Apr 17, 2024 | Syndicated blog