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.
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?
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.
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 }
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:
The last line: _contentService.SaveAndPublish(newContent) throws an error:
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.
Hi Fransico
What version of Umbraco are you using?
/Jan
Sorry, I forgot to mention that.
I am using version 6.0.3.
Empty your recycle bin.
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:
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
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?
I don't know what's wrong, I am using 6.1.2
Scott
is working on a reply...