Copied to clipboard

Flag this post as spam?

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


  • Angel 52 posts 109 karma points
    Jul 17, 2015 @ 15:31
    Angel
    0

    ContentService.Saved called when visiting a page

    I am trying to use ContentService.Saved to be able to take action whenever a node is saved in the back end.

    protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                //ContentService.Publishing += ContentServicePublishing;
                ContentService.Saved += ContentServiceSaved;
            }
    
    private void ContentServiceSaved(IContentService sender, SaveEventArgs<IContent> e)
            {
    
                foreach (var node in e.SavedEntities)
                if (node.ContentType.Alias == "post")
                {
                    SendMail(node);
                }
            }
    

    The only problem i find is that when I visit a page (not through the backend), the ContentServiceSaved method gets called. Am i wrong to assume that this should only get called on save in the back end? I wouldn't expect it to be called when the actual page is visited.

    Can someone clarify? Or help?

  • Mark Bowser 273 posts 860 karma points c-trib
    Jul 17, 2015 @ 16:15
    Mark Bowser
    0

    I wouldn't expect that either, and haven't experienced this before. How can you tell that the ContentServiceSaved method is getting called? Are you 100% sure?

    My first guess is that on page load some/all of your pages save some sort of data using the ContentService.

    Do all of your pages call ContentServiceSaved(...)? Or just a couple of them? Try doing a site wide search for ContentService to see if there is some place in your site that you are pragmatically saving content.

    Let me know. I'm curious about this one. Sounds pretty strange.

  • Angel 52 posts 109 karma points
    Jul 17, 2015 @ 16:54
    Angel
    0

    I'm positive it gets called, as it hits the breakpoint when I run in VS.

    And with your comment about guessing...that some sort of save is occurring when the page is loaded ...the struck something.

    I DO save when a page is loaded as I'm doing some sort of 'page visited' counter which updates the pagevisit property on a page and saves it on page load!

    That is TOTALLY it I'm sure.

  • Angel 52 posts 109 karma points
    Jul 17, 2015 @ 17:04
    Angel
    0

    Question is - would there be a way I could tell when a node is published via code vs via the backend?

    I'd like to be able to filter my actions based on where the save was triggered.

    Any ideas?

  • Sebastiaan Janssen 5057 posts 15514 karma points MVP admin hq
    Jul 18, 2015 @ 15:07
    Sebastiaan Janssen
    0

    Please please please do NOT hold a counter on your document that saves every time you visit a page! Not only is it going to make the page load slow it is also creating a new version for that document, re-saving all of the properties you have on the page. When you go live with this site you will see the database grow very quickly and perfomance go down a lot.

    I recommend you implement a counter by creating your own database table and log visits there.

    And to answer your last question: there's no way to tell if a node is saved from the backoffice or through some other way, making the above advice even more applicable. :)

  • Mike Chambers 636 posts 1253 karma points c-trib
    Jul 19, 2015 @ 08:29
    Mike Chambers
    0

    Altough yeah a counter on content, not all that good a pattern.

    As to knowing whether backoffice user saved or not... doesn't the contentService save have an authenticating admin user associated with the call?

    If so then just have an umbraco user for your coded front end saves to authenticate against, and check for that user as the last updater to distinguish between front end and back office?

  • Sebastiaan Janssen 5057 posts 15514 karma points MVP admin hq
    Jul 19, 2015 @ 09:40
    Sebastiaan Janssen
    0

    Yes, but all saves have an admin associated with it and sure you can create a "fake" user and pass that on when saving on the frontend.

    But again: Please don't do it, it's going to be a world of hurt.

Please Sign in or register to post replies

Write your reply to:

Draft