Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Maxim B 9 posts 69 karma points
    Oct 30, 2014 @ 10:41
    Maxim B
    0

    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?

    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);
                        }
                    }
                }
            }
        }
  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    Oct 30, 2014 @ 16:53
    Jan Skovgaard
    0

    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

  • Maxim B 9 posts 69 karma points
    Oct 30, 2014 @ 17:29
    Maxim B
    0

    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.

     

     

  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    Oct 30, 2014 @ 17:34
    Jan Skovgaard
    0

    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

  • Maxim B 9 posts 69 karma points
    Oct 30, 2014 @ 17:56
    Maxim B
    0

    1311 is the node that needs to be moved.

  • Charles Afford 1163 posts 1709 karma points
    Nov 03, 2014 @ 21:13
    Charles Afford
    0

    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 :)

  • Marchino 1 post 21 karma points
    Nov 07, 2014 @ 16:47
    Marchino
    0

    Hi Charlie,

    could you post here an example of your solution?

    I have the same problem of Maxim B

    Many thanks

    Marco

  • Charles Afford 1163 posts 1709 karma points
    Nov 07, 2014 @ 17:14
    Charles Afford
    0

    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 {

       public bool returnFromEvent = false;
    

    protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) { ContentService.Saved += ContentServiceOnSaving; }

    private void ContentServiceOnSaving(IContentService sender, SaveEventArgs

    }
    

    Does that make sense?

    Charlie :)

  • Maxim B 9 posts 69 karma points
    Nov 08, 2014 @ 13:22
    Maxim B
    0

    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:

    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;
                }
            }
        }
    
  • Charles Afford 1163 posts 1709 karma points
    Nov 08, 2014 @ 18:30
    Charles Afford
    0

    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

  • Oscar Svedberg 19 posts 82 karma points
    Dec 11, 2014 @ 02:35
    Oscar Svedberg
    101

    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!

    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);
                    }
                }
            }
        }
    
  • Charles Afford 1163 posts 1709 karma points
    Dec 12, 2014 @ 13:18
    Charles Afford
    0

    Thanks Oscar

  • Maxim B 9 posts 69 karma points
    Jan 20, 2015 @ 15:32
    Maxim B
    0

    Respect, Oscar

  • Oscar Svedberg 19 posts 82 karma points
    Jan 20, 2015 @ 19:43
    Oscar Svedberg
    0

    If you can and feel like it, please set my answer as final solution :)

Please Sign in or register to post replies

Write your reply to:

Draft