Copied to clipboard

Flag this post as spam?

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


  • James Joplin 11 posts 62 karma points
    Feb 05, 2013 @ 00:44
    James Joplin
    0

    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 :

     public class Global : UmbracoApplication
        {
            protected override void 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

    <%@ Application Codebehind="Global.asax.cs" Inherits="Project.Name.Web.Global" Language="C#" %>
    

    Am I missing something here?

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Feb 05, 2013 @ 12:19
    Jeroen Breuer
    0

    What happens if you try to register the events with IApplicationEventHandler? http://our.umbraco.org/documentation/reference/Events/

    Jeroen

  • James Joplin 11 posts 62 karma points
    Feb 05, 2013 @ 15:52
    James Joplin
    0

    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?

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Feb 05, 2013 @ 16:07
    Jeroen Breuer
    0

    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

  • James Joplin 11 posts 62 karma points
    Feb 05, 2013 @ 16:53
    James Joplin
    0

    Hey Jeroen,

    Thanks so much for the feedback! Inspecting the ContentService class shows this metadata

            // Summary:
            //     Occurs after Copy
            public static event TypedEventHandler<IContentServiceCopyEventArgs<IContent>> Copied;
            //
            // Summary:
            //     Occurs before Copy
            public static event TypedEventHandler<IContentServiceCopyEventArgs<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).
            public static event TypedEventHandler<IContentServiceNewEventArgs<IContent>> Created;
            //
            // Summary:
            //     Occurs before Create
            public static event TypedEventHandler<IContentServiceNewEventArgs<IContent>> Creating;
            //
            // Summary:
            //     Occurs after Delete
            public static event TypedEventHandler<IContentServiceDeleteEventArgs<IContent>> Deleted;
            //
            // Summary:
            //     Occurs after Delete Versions
            public static event TypedEventHandler<IContentServiceDeleteRevisionsEventArgs> DeletedVersions;
            //
            // Summary:
            //     Occurs before Delete
            public static event TypedEventHandler<IContentServiceDeleteEventArgs<IContent>> Deleting;
            //
            // Summary:
            //     Occurs before Delete Versions
            public static event TypedEventHandler<IContentServiceDeleteRevisionsEventArgs> DeletingVersions;
            //
            // Summary:
            //     Occurs after Move
            public static event TypedEventHandler<IContentServiceMoveEventArgs<IContent>> Moved;
            //
            // Summary:
            //     Occurs before Move
            public static event TypedEventHandler<IContentServiceMoveEventArgs<IContent>> Moving;
            //
            // Summary:
            //     Occurs after Rollback
            public static event TypedEventHandler<IContentServiceRollbackEventArgs<IContent>> RolledBack;
            //
            // Summary:
            //     Occurs before Rollback
            public static event TypedEventHandler<IContentServiceRollbackEventArgs<IContent>> RollingBack;
            //
            // Summary:
            //     Occurs after Save
            public static event TypedEventHandler<IContentServiceSaveEventArgs<IContent>> Saved;
            //
            // Summary:
            //     Occurs before Save
            public static event TypedEventHandler<IContentServiceSaveEventArgs<IContent>> Saving;
            //
            // Summary:
            //     Occurs before Send to Publish
            public static event TypedEventHandler<IContentServiceSendToPublishEventArgs<IContent>> SendingToPublish;
            //
            // Summary:
            //     Occurs after Send to Publish
            public static event TypedEventHandler<IContentServiceSendToPublishEventArgs<IContent>> SentToPublish;
            //
            // Summary:
            //     Occurs after Content is moved to Recycle Bin
            public static event TypedEventHandler<IContentServiceMoveEventArgs<IContent>> Trashed;
            //
            // Summary:
            //     Occurs before Content is moved to Recycle Bin
            public static event TypedEventHandler<IContentServiceMoveEventArgs<IContent>> Trashing;

    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?

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Feb 05, 2013 @ 17:01
    Jeroen Breuer
    0

    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

  • James Joplin 11 posts 62 karma points
    Feb 05, 2013 @ 17:03
    James Joplin
    0

    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

  • Francisco 21 posts 72 karma points
    Mar 22, 2013 @ 03:51
    Francisco
    0

    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,

     

     

  • Francisco 21 posts 72 karma points
    Mar 22, 2013 @ 05:05
    Francisco
    0

    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?

     

     


     

  • James Joplin 11 posts 62 karma points
    Mar 22, 2013 @ 05:26
    James Joplin
    0

    In our specific need we are utilizing wkhtmltopdf to generate pdf files, I'm providing code snippets.

    EventHandler.cs

     public class EventHandler : IApplicationEventHandler

    .....

     /// <summary>
            /// Called when [application starting].
            /// </summary>
            /// <param name="httpApplication">The HTTP application.</param>
            /// <param name="applicationContext">The application context.</param>
            public void OnApplicationStarting(UmbracoApplicationBase httpApplication, ApplicationContext applicationContext)
            {   
                // umbraco 4 document event for after publish
                Document.AfterPublish += new Document.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, ditch
                int courseRootId = Convert.ToInt32(ConfigurationManager.AppSettings["CourseRootId"]);
                if (courseRootId != sender.ParentId)
                    return;
    
                ProcessStartInfo psi = new ProcessStartInfo();
                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 save
                string pdfPath = HttpContext.Current.Server.MapPath("~/media/course-pdf/");
                pdfPath = pdfPath + sender.Id + ".pdf";
    
                // build the path to the server for us to pass to wkhtmltopdf
                string 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.

  • Francisco 21 posts 72 karma points
    Mar 24, 2013 @ 21:47
    Francisco
    0

    "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.

     

     

Please Sign in or register to post replies

Write your reply to:

Draft