Copied to clipboard

Flag this post as spam?

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


  • Julian 2 posts 23 karma points
    Mar 20, 2015 @ 16:54
    Julian
    1

    Checking node for updated properties

    Hi, 

    I'm using Umbraco 6.1.6.

    I'm trying to check the current node as it is saved to see which properties have changed, basically I want to execute some code if the only changes to my node are in 4 specific properties. I'm firing my code in the ContentService.Saving event, getting all changed nodes and iterating through the properties on each node, if the property is not one of 4 properties that I wish to ignore and has been updated (IsDirty()) then I know not to run the code for this node. This all seems to work as expected once the node has been opened and saved once, but when I first open the node and save it (without changing any content) a couple of the properties are marked as Dirty even though they haven't changed, UmbracoNaviHide is one of them, the other is a ContentPicker. When I look at the "_Propchangedinfo" for the UmbracoNaviHide property as I step through the code there is an entry in there to set "Value" to "true" even though I haven't changed anything, but once the node is saved the value has not been updated! Something else that I have noticed is that if the contentpicker property has a value before I open the node then it isn't markled as dirty.

    I have posted my code below

    TIA 

    Julian

    using Umbraco.Core;

    using Umbraco.Core.Events;

    using Umbraco.Core.Models;

    using Umbraco.Core.Publishing;

    using Umbraco.Core.Services;

    using Umbraco.Core.Models.Membership;

    using System.Collections.Generic;

        public class UmbracoEventHandlers : ApplicationEventHandler

        {

            // keep a record of the nodes that we need to reset the "WriterProfile" for

            Dictionary ResetNodes = new Dictionary();

     

            protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)

            {

                ContentService.Saving += ContentServiceSaving;

                ContentService.Saved += ContentServiceSaved;  

            }

     

            private void ContentServiceSaving(IContentService sender, SaveEventArgs args)

            {

     

                ResetNodes = new Dictionary();

     

                foreach (var node in args.SavedEntities)

                {

                    //if (node.ContentType.Alias == "Comment")

                    List ApprovalProperties = new List() { "BusinessOwner", "ExpiryDate", "ApprovalRejectionNotes", "SendRejectionNotification" };

     

                    if (node.HasProperty("BusinessOwner"))

                    {

                        bool NodeIsDirty = false;

     

                        // check all the properties to make sure that at least one of the 

                        // none approval properties has been updated

                        foreach (Property CurProp in node.Properties)

                        {

                            string PropAlias = CurProp.Alias;

     

                            if (!ApprovalProperties.Contains(PropAlias) && CurProp.IsDirty())

                            {

                                NodeIsDirty = true;

                            }

                        }

     

                        // if the node has only has it's approval properties updated then add it to the list of nodes to reset the last updated user

                        if (!NodeIsDirty)

                        {

                            ResetNodes.Add(node.Id, node.WriterId);

                        }

                    }

                }

            }

     

            private void ContentServiceSaved(IContentService sender, SaveEventArgs args)

            {

                // reset the WriterId for any nodes that have only had the Approval/ Rejection properties changed

                foreach (KeyValuePair CurResetNode in ResetNodes)

                {

                    IContent CurNode = sender.GetById(CurResetNode.Key);

                    CurNode.WriterId = (int)CurResetNode.Value;

                }

            }

        }

    }

Please Sign in or register to post replies

Write your reply to:

Draft