Copied to clipboard

Flag this post as spam?

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


  • Adriano Fabri 459 posts 1602 karma points
    Nov 04, 2015 @ 17:07
    Adriano Fabri
    0

    Hello, I need help to realize, for an Umbraco 7 website, a custom handler to auto-fill a property on new node creation.

    For example...every time I create a new node I immediately must:

    • Check if exist a "title" property and automatically assign the node name value
    • Check if exist a "date" property and automatically assign the Now() value

    I made several attempts but they don't work Can anyone help me? Thank you Adriano

  • Sam 26 posts 137 karma points c-trib
    Nov 10, 2015 @ 09:43
  • Adriano Fabri 459 posts 1602 karma points
    Nov 10, 2015 @ 10:44
    Adriano Fabri
    100

    Yeah...I found the solution last week exactly with the contentService.

    This is an example code:

    using System;
    using Umbraco.Core;
    using Umbraco.Core.Events;
    using Umbraco.Core.Models;
    using Umbraco.Core.Publishing;
    using Umbraco.Core.Services;
    
    namespace myNameSpace
    
    {
        public class myHandler : ApplicationEventHandler
        {
    
            protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                ContentService.Saving += ContentServiceSaving;
            }
    
            private void ContentServiceSaving(IContentService sender, SaveEventArgs<IContent> args)
            {
                foreach (var node in args.SavedEntities)
                {
                    foreach (var property in node.Properties)
                    {
                        if ((property.Alias == "title") && (String.IsNullOrEmpty(property.Value.ToString()))) 
                        { 
                            property.Value = node.Name; 
                        }
    
                        if ((property.Alias == "date") && (String.IsNullOrEmpty(property.Value.ToString())))
                        {
                            property.Value = DateTime.Now;
                        }
                    }
                }
            }
        }
    }
    

    That's all!!!

    Glad to help.

    Adriano

Please Sign in or register to post replies

Write your reply to:

Draft