I need to move a news page after save to another parent. But the save operation is repeated and repeated and repeated and OutOfMemoryException. What the bug?
public class AutoMoveToFolder : ApplicationEventHandler { protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) { ContentService.Saved += ContentServiceOnSaving; } private void ContentServiceOnSaving(IContentService sender, SaveEventArgs<IContent> e) { var newsLibraryContentType = ApplicationContext.Current.Services.ContentTypeService.GetContentType("NewsLibrary"); var yearContentType = ApplicationContext.Current.Services.ContentTypeService.GetContentType("Year"); var monthContentType = ApplicationContext.Current.Services.ContentTypeService.GetContentType("Month"); var dayContentType = ApplicationContext.Current.Services.ContentTypeService.GetContentType("Day"); var root = sender.GetRootContent().FirstOrDefault(); if (root == null) { return; } foreach (var item in e.SavedEntities) { if (item.ContentType.Alias == "NewsPage") { var dateCndidate = item.GetValue("newsDate"); if (dateCndidate == null) { return; } if (string.IsNullOrEmpty(dateCndidate.ToString())) { return; } var date = Convert.ToDateTime(dateCndidate); var library = root.Descendants().FirstOrDefault(p => p.ContentTypeId == newsLibraryContentType.Id); if (library != null) { var year = library.Descendants().FirstOrDefault(p => p.ContentTypeId == yearContentType.Id && p.Name == date.Year.ToString()); if (year == null) { year = sender.CreateContent(date.Year.ToString(), library, yearContentType.Alias); sender.SaveAndPublishWithStatus(year); } var month = year.Descendants().FirstOrDefault(p => p.ContentTypeId == monthContentType.Id && p.Name == date.Month.ToString()); if (month == null) { month = sender.CreateContent(date.Month.ToString(), year, monthContentType.Alias); sender.SaveAndPublishWithStatus(month); } var day = month.Descendants().FirstOrDefault(p => p.ContentTypeId == dayContentType.Id && p.Name == date.Day.ToString()); if (day == null) { day = sender.CreateContent(date.Day.ToString(), month, dayContentType.Alias); sender.SaveAndPublishWithStatus(day); } sender.Move(item, day.Id); } } } } }
How many entries does already exist in the branch where the newsitem is created initially? Seems that you call the Move method for each of those nodes, which could probably lead to some weird behavior.
Hi i have had the same problem and will try and remember what i did. The problem is you are calling that SaveAndPublish method and its calling the event again and again and again. I think what have a bool condition around the whole event.
I tried your method. Your plan is understandable, but it does not work. Event calling again and again. I dont understand why is not working the condition:
if (item.ContentType.Alias == "NewsPage")
My current code.
public class AutoMoveToFolder : ApplicationEventHandler
{
private bool _returnFromEvent = false;
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
ContentService.Saved += ContentServiceOnSaving;
}
private void ContentServiceOnSaving(IContentService sender, SaveEventArgs<IContent> e)
{
if (!_returnFromEvent)
{
var newsLibraryContentType = ApplicationContext.Current.Services.ContentTypeService.GetContentType("NewsLibrary");
var yearContentType = ApplicationContext.Current.Services.ContentTypeService.GetContentType("Year");
var monthContentType = ApplicationContext.Current.Services.ContentTypeService.GetContentType("Month");
var dayContentType = ApplicationContext.Current.Services.ContentTypeService.GetContentType("Day");
var root = sender.GetRootContent().FirstOrDefault();
if (root == null)
{
return;
}
foreach (var item in e.SavedEntities)
{
if (item.ContentType.Alias == "NewsPage")
{
var dateCndidate = item.GetValue("newsDate");
if (dateCndidate == null)
{
return;
}
if (string.IsNullOrEmpty(dateCndidate.ToString()))
{
return;
}
var date = Convert.ToDateTime(dateCndidate);
var library = root.Descendants().FirstOrDefault(p => p.ContentTypeId == newsLibraryContentType.Id);
if (library != null)
{
var year = library.Descendants().FirstOrDefault(p => p.ContentTypeId == yearContentType.Id && p.Name == date.Year.ToString());
if (year == null)
{
year = sender.CreateContent(date.Year.ToString(), library, yearContentType.Alias);
_returnFromEvent = true;
sender.SaveAndPublishWithStatus(year);
}
var month = year.Descendants().FirstOrDefault(p => p.ContentTypeId == monthContentType.Id && p.Name == date.Month.ToString());
if (month == null)
{
month = sender.CreateContent(date.Month.ToString(), year, monthContentType.Alias);
_returnFromEvent = true;
sender.SaveAndPublishWithStatus(month);
}
var day = month.Descendants().FirstOrDefault(p => p.ContentTypeId == dayContentType.Id && p.Name == date.Day.ToString());
if (day == null)
{
day = sender.CreateContent(date.Day.ToString(), month, dayContentType.Alias);
_returnFromEvent = true;
sender.SaveAndPublishWithStatus(day);
}
sender.Move(item, day.Id);
}
}
}
}
else
{
return;
}
}
}
Not sure if you solved your problem, but I've just spent numerous hours with the exact same problem and came up with this solution which works pretty well!
public class DateHandler : IApplicationEventHandler
{
private static readonly ILog Log = LogManager.GetLogger(typeof(DateHandler));
public void OnApplicationInitialized(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
}
public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
}
public void OnApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
ContentService.Published += ContentService_Published;
}
private 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 == "NewsArticle")
{
var yearPageName = item.CreateDate.Year.ToString();
var monthPageName = item.CreateDate.ToString("MMMM");
var archiveRoot = item.TraverseWhileContentType("NewsArchive");
var year = archiveRoot.Children().SingleOrDefault(c => c.Name == yearPageName);
if (year == null)
{
year = new Content(yearPageName, archiveRoot, archiveRoot.ContentType);
if (!ApplicationContext.Current.Services.ContentService.Publish(year))
{
// TODO : Log error
}
}
var month = year.Children().SingleOrDefault(c => c.Name == monthPageName);
if (month == null)
{
month = new Content(monthPageName, year, archiveRoot.ContentType);
if (!ApplicationContext.Current.Services.ContentService.Publish(month))
{
// TODO : Log error
}
}
if(item.ParentId == month.Id)
{
return;
}
ApplicationContext.Current.Services.ContentService.Move(item, month.Id);
}
}
}
}
Move content after save. It's possible?
Hi guys.
Umbraco 6.2.4.
I need to move a news page after save to another parent. But the save operation is repeated and repeated and repeated and OutOfMemoryException. What the bug?
Hi Maxim
How many entries does already exist in the branch where the newsitem is created initially? Seems that you call the Move method for each of those nodes, which could probably lead to some weird behavior.
/Jan
Hi Jan.
Seems that you call the Move method for each of those nodes, which could probably lead to some weird behavior.
I read the trace log. Umbraco calls save method for only 1311 page. Very strange behavior.
2014-10-30 19:17:11,265 [6] INFO Umbraco.Core.Publishing.PublishingStrategy - [Thread 12] Content 'partners' with Id '1311' has been published.
2014-10-30 19:17:11,419 [6] INFO Umbraco.Core.Publishing.PublishingStrategy - [Thread 12] Content 'partners' with Id '1311' has been published.
2014-10-30 19:17:11,575 [6] INFO Umbraco.Core.Publishing.PublishingStrategy - [Thread 12] Content 'partners' with Id '1311' has been published.
2014-10-30 19:17:11,735 [6] INFO Umbraco.Core.Publishing.PublishingStrategy - [Thread 12] Content 'partners' with Id '1311' has been published.
2014-10-30 19:17:11,900 [6] INFO Umbraco.Core.Publishing.PublishingStrategy - [Thread 12] Content 'partners' with Id '1311' has been published.
2014-10-30 19:17:12,054 [6] INFO Umbraco.Core.Publishing.PublishingStrategy - [Thread 12] Content 'partners' with Id '1311' has been published.
Hi Maxim
So 1311 is the id of, which node? Is it where the items are created? Or the rootnode where they should be moved to?
/Jan
1311 is the node that needs to be moved.
Hi i have had the same problem and will try and remember what i did. The problem is you are calling that SaveAndPublish method and its calling the event again and again and again. I think what have a bool condition around the whole event.
If(some bool = false) { Your event }
else { return }
Before i did the SaveAndPublish() event i set a Boolean. Thus when i drop back into the SaveAndPublish() event would go to the else and return.
Hope this helps
Charlie :)
Hi Charlie,
could you post here an example of your solution?
I have the same problem of Maxim B
Many thanks
Marco
Sure Marchion
So you have at the moment
public class AutoMoveToFolder : ApplicationEventHandler { protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) { ContentService.Saved += ContentServiceOnSaving; } private void ContentServiceOnSaving(IContentService sender, SaveEventArgs
What i did was this:
public class AutoMoveToFolder : ApplicationEventHandler {
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) { ContentService.Saved += ContentServiceOnSaving; }
private void ContentServiceOnSaving(IContentService sender, SaveEventArgs
Does that make sense?
Charlie :)
Hello Charlie.
I tried your method. Your plan is understandable, but it does not work. Event calling again and again. I dont understand why is not working the condition:
My current code.
What happens if you don't have the else statement? Have you debugged, what is the value of the bool when you hit the event again?
Charlie
Hi!
Not sure if you solved your problem, but I've just spent numerous hours with the exact same problem and came up with this solution which works pretty well!
Thanks Oscar
Respect, Oscar
If you can and feel like it, please set my answer as final solution :)
is working on a reply...