I have been trying to setup a post publish event for some logic that needs to happen when a page is published. Consider the following code :
publicclassGlobal : UmbracoApplication
{
protectedoverridevoid OnApplicationStarted(object sender, EventArgs e)
{
base.OnApplicationStarted(sender, e);
// force the site to re sync the xml on startup// this would need to happen if we pushed a database change// to the environment var contentService = ApplicationContext.Current.Services.ContentService;
contentService.RePublishAll();
umbraco.library.RefreshContent();
//setup our post publishing hooks so we can save pdfs after a course is saved ContentService.SentToPublish += ContentService_SentToPublish;
ContentService.SendingToPublish += ContentService_SendingToPublish;
ContentService.Saved += ContentService_Saved;
//Document.AfterPublish += new Document.PublishEventHandler(Document_AfterPublish);
}
void ContentService_Saved(IContentService sender, Umbraco.Core.Events.SaveEventArgs<IContent> e)
{
var foo = "bar";
}
void ContentService_SendingToPublish(IContentService sender, Umbraco.Core.Events.SendToPublishEventArgs<IContent> e)
{
var foo = "bar";
}
void ContentService_SentToPublish(IContentService sender, Umbraco.Core.Events.SendToPublishEventArgs<IContent> e)
{
var contentType = e.Entity.ContentType;
var foo = "bar";
}
As you can see I am attempting to attach an event handler to the publishing events, but they are not being called. Even stranger the "Saved" event is being called twice.
The code referenced above is in our global.asax.cs file and is appropriately reference in the global.asax markup
Hmm I don't think it is. I think SentToPublish is when someone doesn't have publish rights and can only setup things for someone else to publish. So you might need another event.
Thanks so much for the feedback! Inspecting the ContentService class shows this metadata
// Summary:// Occurs after CopypublicstaticeventTypedEventHandler<IContentService, CopyEventArgs<IContent>> Copied;
//// Summary:// Occurs before CopypublicstaticeventTypedEventHandler<IContentService, CopyEventArgs<IContent>> Copying;
//// Summary:// Occurs after Create//// Remarks:// Please note that the Content object has been created, but not saved so it// does not have an identity yet (meaning no Id has been set).publicstaticeventTypedEventHandler<IContentService, NewEventArgs<IContent>> Created;
//// Summary:// Occurs before CreatepublicstaticeventTypedEventHandler<IContentService, NewEventArgs<IContent>> Creating;
//// Summary:// Occurs after DeletepublicstaticeventTypedEventHandler<IContentService, DeleteEventArgs<IContent>> Deleted;
//// Summary:// Occurs after Delete VersionspublicstaticeventTypedEventHandler<IContentService, DeleteRevisionsEventArgs> DeletedVersions;
//// Summary:// Occurs before DeletepublicstaticeventTypedEventHandler<IContentService, DeleteEventArgs<IContent>> Deleting;
//// Summary:// Occurs before Delete VersionspublicstaticeventTypedEventHandler<IContentService, DeleteRevisionsEventArgs> DeletingVersions;
//// Summary:// Occurs after MovepublicstaticeventTypedEventHandler<IContentService, MoveEventArgs<IContent>> Moved;
//// Summary:// Occurs before MovepublicstaticeventTypedEventHandler<IContentService, MoveEventArgs<IContent>> Moving;
//// Summary:// Occurs after RollbackpublicstaticeventTypedEventHandler<IContentService, RollbackEventArgs<IContent>> RolledBack;
//// Summary:// Occurs before RollbackpublicstaticeventTypedEventHandler<IContentService, RollbackEventArgs<IContent>> RollingBack;
//// Summary:// Occurs after SavepublicstaticeventTypedEventHandler<IContentService, SaveEventArgs<IContent>> Saved;
//// Summary:// Occurs before SavepublicstaticeventTypedEventHandler<IContentService, SaveEventArgs<IContent>> Saving;
//// Summary:// Occurs before Send to PublishpublicstaticeventTypedEventHandler<IContentService, SendToPublishEventArgs<IContent>> SendingToPublish;
//// Summary:// Occurs after Send to PublishpublicstaticeventTypedEventHandler<IContentService, SendToPublishEventArgs<IContent>> SentToPublish;
//// Summary:// Occurs after Content is moved to Recycle BinpublicstaticeventTypedEventHandler<IContentService, MoveEventArgs<IContent>> Trashed;
//// Summary:// Occurs before Content is moved to Recycle BinpublicstaticeventTypedEventHandler<IContentService, MoveEventArgs<IContent>> Trashing;
Hmm I don't think it is. I think SentToPublish is when someone doesn't have publish rights and can only setup things for someone else to publish. So you might need another event.
{quote}
Anyone knows if ContentService.SendingToPublish is equivalent of Document.BeforePublish ??? If not, which one is?
///<summary>/// Called when [application starting].///</summary>///<param name="httpApplication">The HTTP application.</param>///<param name="applicationContext">The application context.</param>publicvoid OnApplicationStarting(UmbracoApplicationBase httpApplication, ApplicationContext applicationContext)
{
// umbraco 4 document event for after publishDocument.AfterPublish += newDocument.PublishEventHandler(Document_AfterPublish);
// umbraco 6, incorrect event for "after" publish//ContentService.SentToPublish += ContentService_SentToPublish;
}
---
///<summary>/// Event to handle our own logic after the document is published///</summary>///<param name="sender">The sender.</param>///<param name="e">The <see cref="PublishEventArgs" /> instance containing the event data.</param>void Document_AfterPublish(Document sender, PublishEventArgs e)
{
// if we aren't a course, ditchint courseRootId = Convert.ToInt32(ConfigurationManager.AppSettings["CourseRootId"]);
if (courseRootId != sender.ParentId)
return;
ProcessStartInfo psi = newProcessStartInfo();
psi.FileName = HttpContext.Current.Server.MapPath("~/bin/wkhtmltopdf/wkhtmltopdf.exe");
psi.WorkingDirectory = HttpContext.Current.Server.MapPath("~/bin/wkhtmltopdf/");
psi.UseShellExecute = false;
// build the path for the pdf to savestring pdfPath = HttpContext.Current.Server.MapPath("~/media/course-pdf/");
pdfPath = pdfPath + sender.Id + ".pdf";
// build the path to the server for us to pass to wkhtmltopdfstring documentPath = HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Authority + umbraco.library.NiceUrl(sender.Id);
psi.Arguments = "--print-media-type " + documentPath + " " + pdfPath;
// run our pdf conversion
System.Diagnostics.Process.Start(psi);
}
Not perfect and using "4.0" code, but it gets the job done for us. If the event is ever added we can simply adjust the event handler as appropriate.
"Not perfect and using "4.0" code, but it gets the job done for us. If the event is ever added we can simply adjust the event handler as appropriate."
Not really, because ContentService.SendingToPublish per example, gives you a IContentService, and a IContent. While Document.After gives you a Document, which works differently.
V6 ContentService Publish Event
I have been trying to setup a post publish event for some logic that needs to happen when a page is published. Consider the following code :
As you can see I am attempting to attach an event handler to the publishing events, but they are not being called. Even stranger the "Saved" event is being called twice.
The code referenced above is in our global.asax.cs file and is appropriately reference in the global.asax markup
Am I missing something here?
What happens if you try to register the events with IApplicationEventHandler? http://our.umbraco.org/documentation/reference/Events/
Jeroen
Hey Jeroen,
No luck there either.
Perhaps I am utilizing this incorrectly.
I want to kick off a wkhtmltopdf process once a page is published. Is the 'SentToPublish' event the correct one to listen for?
Hmm I don't think it is. I think SentToPublish is when someone doesn't have publish rights and can only setup things for someone else to publish. So you might need another event.
Jeroen
Hey Jeroen,
Thanks so much for the feedback! Inspecting the ContentService class shows this metadata
I don't see anything that fires before/after an item is published. If I want to make a feature request I assume the proper place is here : http://our.umbraco.org/contribute/report-an-issue-or-request-a-feature
Is that correct?
Yes feature request can be added at http://issues.umbraco.org/dashboard#newissue=yes but there should already be an event after something is saved. Now I'm not sure which it should be.
Jeroen
If there is a property I overlooked on anything that the 'save' event would receive please let me know, that message is coming through fine! haha.
Thanks for your help.
james
Hi James,
I am running into the same issue here. Did you find a solution?
I am using Umbraco v6.0.2, and the ContentService.SendingToPublish still do not work.
Cheers,
What Jeroen wrote makes sense:
{quote}
Hmm I don't think it is. I think SentToPublish is when someone doesn't have publish rights and can only setup things for someone else to publish. So you might need another event.
{quote}
Anyone knows if ContentService.SendingToPublish is equivalent of Document.BeforePublish ??? If not, which one is?
In our specific need we are utilizing wkhtmltopdf to generate pdf files, I'm providing code snippets.
EventHandler.cs
public class EventHandler : IApplicationEventHandler
.....
---
Not perfect and using "4.0" code, but it gets the job done for us. If the event is ever added we can simply adjust the event handler as appropriate.
"Not perfect and using "4.0" code, but it gets the job done for us. If the event is ever added we can simply adjust the event handler as appropriate."
Not really, because ContentService.SendingToPublish per example, gives you a IContentService, and a IContent. While Document.After gives you a Document, which works differently.
However, thank you for your code.
is working on a reply...