November Happy Hour will be moved to Thursday December 5th.
November Happy Hour will be moved to Thursday December 5th.
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.
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?
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
}
}
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);
}
}
}
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!
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.
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.