Copied to clipboard

Flag this post as spam?

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


  • lj 81 posts 425 karma points
    Aug 21, 2014 @ 11:13
    lj
    0

    best way to run server side code on document save

    Hi,

     

    I have an umbraco 7 instance.

    I have a doc type to witch I have added a checkbox (true/false) data type. What I want to do is if the user checks this and save and publishes i want to the run some custom server code.

    The only way I can think of doing this would be to remove the built in checkbox and replace it with a custom built angularjs one which in turn will use an ajax call to an umbraco web api method which will in turn run some server code. 

    My question is is this the only/best appraoch?

    Thnaks

    lj

  • Dan Lister 416 posts 1974 karma points c-trib
    Aug 21, 2014 @ 11:45
    Dan Lister
    0

    Hi lj,

    You probably want to hook into the Content Service Saved event. From there, you'll be able to run some code based on what an editor is saving:

    using Umbraco.Core;
    using Umbraco.Core.Events;
    using Umbraco.Core.Models;
    using Umbraco.Core.Services;
    
    namespace Application
    {
        public class CustomEventHandler : ApplicationEventHandler
        {
            protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                ContentService.Saved += delegate(IContentService sender, SaveEventArgs<IContent> args)
                {
                    foreach (var content in args.SavedEntities)
                    {
                        if (content.GetValue<bool>("aliasOfTrueFalseProperty"))
                        {
                            // Some custom logic when checked
                       }
                    }
                };
            }
        }
    }
    

    Thanks, Dan.

  • lj 81 posts 425 karma points
    Aug 21, 2014 @ 12:07
    lj
    0

    Hi Dan,

    Would this code run every time any page was saved and not just for pages of a specific doc type?

    Thanks

    lj

     

  • Dan Lister 416 posts 1974 karma points c-trib
    Aug 21, 2014 @ 12:11
    Dan Lister
    100

    Yeah it does but you could change you foreach statement to only loop through pages of a certain document type:

    foreach (var content in args.SavedEntities.Where(c => c.ContentType.Alias.Equals("documentTypeAliasWithCheckboxOn")))
    

    Thanks, Dan.

  • lj 81 posts 425 karma points
    Aug 21, 2014 @ 12:38
    lj
    0

    yeah seems like a good alternative for what im trying to achieve.  For convention what folder would you keep these classes in?

    One other last question i dont know how much experience you have with custom data types and angularjs but is the only way to run server side code from angularjsby making ajax calls to umbraco web api.

     

    Thanks

    lj

  • Dan Lister 416 posts 1974 karma points c-trib
    Aug 21, 2014 @ 12:42
    Dan Lister
    0

    You can put the classes where ever you like really. An Events folder might be good place.

    I've not tried what you want but I'm guessing you'll want to write an Umbraco Web API Service to be triggered from your custom data type when updated.

    Thanks, Dan.

  • lj 81 posts 425 karma points
    Aug 21, 2014 @ 12:45
    lj
    0

    Yeah thats exactly what I thought.

     thanks for the help.

     

    lj

  • Dan Lister 416 posts 1974 karma points c-trib
    Aug 21, 2014 @ 12:52
    Dan Lister
    0

    No worries. Glad I could help :)

    Thanks, Dan.

Please Sign in or register to post replies

Write your reply to:

Draft