Copied to clipboard

Flag this post as spam?

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


  • Tom C 87 posts 222 karma points
    Dec 06, 2016 @ 09:43
    Tom C
    1

    Code to run upon back office page load

    Hi everybody,

    basically what I want to do is, when a new item is created in the back office of a certain documentype, I want to have some code to run to set the value of a textbox field on that item dynamically (say to be the name of its parent node). Could anyone tell me roughly how I'd go about doing this?

    thanks a lot for any help

  • Jonathan Richards 288 posts 1742 karma points MVP
    Dec 06, 2016 @ 10:20
    Jonathan Richards
    100

    Hi Tom

    You are looking for PublishingStrategy

    public class Register : ApplicationEventHandler
    {
        protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            Umbraco.Core.Publishing.PublishingStrategy.Publishing += PublishingStrategy_Publishing;
        }
    
        private void PublishingStrategy_Publishing(Umbraco.Core.Publishing.IPublishingStrategy sender, Umbraco.Core.Events.PublishEventArgs<IContent> e)
        {
            foreach (var content in e.PublishedEntities.Where(x => x.ContentType.Alias == "MyDocTypeAlias"))
            {
                content.SetValue("MyPropertyAlias", "My New Value Everytime Someone Presses Publish");
            }
        }
    }
    

    Cheers

  • Tom C 87 posts 222 karma points
    Dec 06, 2016 @ 11:04
    Tom C
    0

    Thanks a lot, that worked fine! Ideally I was hoping it would happen on the creation of the page rather than the publishing but for me this does the job well enough. Thanks again.

  • Heather Floyd 582 posts 892 karma points MVP 4x c-trib
    Aug 16, 2022 @ 16:42
    Heather Floyd
    0

    FYI - For Umbraco 9+, you can use the EditorModel Notifications to set an initial value on Node loading.

Please Sign in or register to post replies

Write your reply to:

Draft