Copied to clipboard

Flag this post as spam?

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


  • Francisco 21 posts 72 karma points
    Apr 04, 2013 @ 04:46
    Francisco
    0

    ContentService.SaveAndPublish inside a Document.AfterPublish method throws an error.

    When the user creates a document of a specified document type, I am trying to create a child document within the document created.

    My code:

            private void Document_AfterPublish(Document sender, PublishEventArgs e)
            {
                var contentService = ApplicationContext.Current.Services.ContentService;
                var content = contentService.GetById(sender.Id);
    
                if (!content.IsEventCalendarSubsite())
                    return;
    
                var eventCalendarService = new EventCalendarService(contentService);
    
                var newContent = contentService.CreateContent("details", content, "EventCalendarProxy", content.CreatorId);
    
                contentService.SaveAndPublish(newContent);
    
        }
    

     

    The last line: _contentService.SaveAndPublish(newContent) throws an error:

    System.ArgumentNullException was unhandled by user code
      HResult=-2147467261
      Message=Value cannot be null.
    Parameter name: attribute
      Source=System.Xml.Linq
      ParamName=attribute
      StackTrace:
           at System.Xml.Linq.XAttribute.op_Explicit(XAttribute attribute)
           at UmbracoExamine.UmbracoContentIndexer.ReIndexNode(XElement node, String type)
           at Examine.ExamineManager._ReIndexNode(XElement node, String type, IEnumerable`1 providers)
           at umbraco.content.DocumentCacheEventHandler.Invoke(Document sender, DocumentCacheEventArgs e)
           at umbraco.content.UpdateDocumentCache(Document d)
           at Umbraco.Web.Strategies.Publishing.UpdateCacheAfterPublish.UpdateSingleContentCache(IContent content)
           at Umbraco.Core.Services.ContentService.SaveAndPublishDo(IContent content, Boolean omitCacheRefresh, Int32 userId, Boolean raiseEvents)
           at Umbraco.Core.Services.ContentService.Publish(IContent content, Int32 userId)
           at SLSNZ.Umbraco.Website.EventCalendar.EventCalendarEventHandler.Document_AfterPublish(Document sender, PublishEventArgs e)
           at umbraco.cms.businesslogic.web.Document.PublishEventHandler.Invoke(Document sender, PublishEventArgs e)
           at umbraco.cms.businesslogic.web.Document.PublishWithResult(User u)
           at umbraco.cms.presentation.editContent.Publish(Object sender, EventArgs e)
           at System.EventHandler.Invoke(Object sender, EventArgs e)
           at System.Web.UI.WebControls.ImageButton.OnClick(ImageClickEventArgs e)
           at System.Web.UI.WebControls.ImageButton.RaisePostBackEvent(String eventArgument)
           at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
      InnerException: 
    
    

     

    If I change from Document_AfterPublish to Document_BeforePublish, it doesn't throws a error, but it doesn't publish the child document (probably because the parent has not been published yet).

    If I change from  contentService.SaveAndPublish(newContent) to  contentService.Save(newContent). It saves, but obviously doesn't publish.

    Any thoughts?

    Thanks.

     

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Apr 04, 2013 @ 23:35
    Jan Skovgaard
    0

    Hi Fransico

    What version of Umbraco are you using?

    /Jan

  • Francisco 21 posts 72 karma points
    Apr 05, 2013 @ 00:53
    Francisco
    0

    Sorry, I forgot to mention that.

    I am using version 6.0.3.

     

  • Yakov Lebski 591 posts 2346 karma points
    Apr 29, 2013 @ 10:29
    Yakov Lebski
    0

    Empty your recycle bin.

     

  • Morten Christensen 596 posts 2773 karma points admin hq c-trib
    Apr 29, 2013 @ 11:03
    Morten Christensen
    0

    Since you are using the new services in v6 I'd recommend that you use the events exposed by these services, so you don't mix the new with the old. Try this instead:

    public UpdateAfterPublish()
    {
        PublishingStrategy.Published += PublishingStrategy_Published;
    }
    private void PublishingStrategy_Published(IPublishingStrategy sender, PublishEventArgs<IContent> e)
    {
        if (e.PublishedEntities.Any())
        {
            var currentContent = e.PublishedEntities.First();
            if (!currentContent.IsEventCalendarSubsite())
              return;
           
            var contentService = ApplicationContext.Current.Services.ContentService;
            var eventCalendarService = new EventCalendarService(contentService);//Not sure what this is used for?
           
            var newContent = contentService.CreateContent("details", currentContent, "EventCalendarProxy", currentContent.CreatorId);
            contentService.SaveAndPublish(newContent);
            //Possible workaround:
            //contentService.Save(newContent);
            //contentService.Publish(newContent);
        }
    }

     

    Please note the workaround that is commented out. I believe you have to use this instead of SaveAndPublish because of a bug/oversight that I just noticed while investigating this.

     

    - Morten

  • Scott 95 posts 277 karma points
    Jul 17, 2013 @ 17:25
    Scott
    0

    Hi Morten,

    I have been trying this however with no luck when coming to media items.

    I want to copy the media file to another folder but I cannot seem to get sender.id from media server?

    public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
    {
        MediaService.Saved += MediaService_Saved;
    }
     
    void MediaService_Saved(IMediaService sender, Umbraco.Core.Events.SaveEventArgs<Umbraco.Core.Models.IMedia> e)
    {
    int id = sender.Id; // Id does not exist
    }

    I don't know what's wrong, I am using 6.1.2

    Scott

Please Sign in or register to post replies

Write your reply to:

Draft