Try our conversational search powered by Generative AI!

CreatedContent Event firing twice

Vote:
 

Having this code:

private void CreatedContent(object sender, ContentEventArgs e)
{
    var a = e.Content is ArticlePage;
    if (a) {

        ...

    }

}

While debugging it goes twice returning "a = true" at first and "a = false" at second.

Why could possibly it be happening?

Eventually, code under "if" never gets executed.

#200869
Edited, Jan 29, 2019 16:49
Vote:
 

Do you have any other events that automatically creates new content or do you create new content in your code under "if"? That would also fire the CreatedContent event.

#200910
Jan 30, 2019 14:05
Vote:
 

Hi Mattias, 

thanks for answering.

The whole code is:

private void CreatedContent(object sender, ContentEventArgs e)
{
    var a = e.Content is ArticlePage;
    if (a) {

        var repository = ServiceLocator.Current.GetInstance<IContentRepository>();
        var page = repository.Get<ArticlePage>(e.ContentLink);

        PageData page_clone = page.CreateWritableClone();
        page_clone.Property["PagePeerOrder"].Value = Int32.Parse(page.GetPropertyValue("PagePeerOrder")) - 5;

        EPiServer.DataFactory.Instance.Save(page, EPiServer.DataAccess.SaveAction.SkipValidation);

    }

}

So what I want to do is to change "Sort Index" of copy when it is being pasted. It works, but only when not included into "if" clause, but I need that "if".

So I believe this part EPiServer.DataFactory.Instance.Save causes second loop?

#200911
Edited, Jan 30, 2019 14:13
Vote:
 

Hi

You should use CreatingContent instead of CreatedContent event.

Then the e.Content is allready in writeable mode and you can just set the variables as you want and don't need to do your own Save.

Something like this should do

private void CreatingContent(object sender, ContentEventArgs e)
{
    var a = e.Content as ArticlePage;
    if (a != null) {
        a.PagePeerOrder -= 5
    }
}

#200912
Edited, Jan 30, 2019 14:46
Vote:
 

I think this is for copy events, and then the event triggering doesn't work like expected. I would suggest this code:

private void CreatedContent(object sender, ContentEventArgs e)
{
    var copyContentEventArgs = e as CopyContentEventArgs;

    if (copyContentEventArgs != null)
    {
        var contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();

        if (contentRepository.TryGet<ArticlePage>(copyContentEventArgs.ContentLink, out var articlePage))
        {
            var writableArticlePage = articlePage.CreateWritableClone();
            writableArticlePage.SortIndex -= 5;
            contentRepository.Save(writableArticlePage, SaveAction.ForceCurrentVersion);
        }
    }
}
#200913
Edited, Jan 30, 2019 14:56
Vote:
 

Mattias,

I tried your first solution, and it looks like working!  I will take a deeper look at it and test in any ways, thank you!

Sebbe,

thank you! Will take a look as well.

Cheers guys!

#200916
Edited, Jan 30, 2019 15:17
Vote:
 

When a page is copied, it seems like the properties are updated from the source page after the CreatingContent event occurs. So I don't think the CreatingContent event can be used in your case.

#200918
Jan 30, 2019 15:36
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.