Copied to clipboard

Flag this post as spam?

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


  • Warren Buckley 2106 posts 4836 karma points MVP ∞ admin hq c-trib
    Apr 09, 2009 @ 13:53
    Warren Buckley
    0

    How do I check if a property value has changed when saving in API?

    Hello all.

    I am currently working on a V4 event to stop certain umbraco user roles from changing the value of a property of a particular document type.

    Here is the code I currently have so far

    [code]
    void Document_BeforeSave(Document sender, SaveEventArgs e)
    {
    //GET the current user logged into umbraco
    User currentUser = umbraco.helper.GetCurrentUmbracoUser();

    if (currentUser.UserType.Alias == "protoEditor")
    {
    //WE ARE THE protoEditor type

    //Check IF the groupName property's value has been changed
    if(sender.getProperty("groupName").VersionId != ..?..)
    {
    //Lets set the value back to it's original value
    sender.getProperty("groupName").Value = "";
    }
    }
    }
    [/code]

    I need some help with the IF and how to set the value back to it's original value.

    Thanks,
    Warren

  • Richard Soeteman 4035 posts 12842 karma points MVP
    Apr 09, 2009 @ 14:15
    Richard Soeteman
    0

    Hi Warren

    Can't you just Cancel the event? [code]e.Cancel = true[/code]

    Then the document doesn't get saved at all and the editor get some sort of error message that something was wrong.

    Cheers,

    Richard

  • Warren Buckley 2106 posts 4836 karma points MVP ∞ admin hq c-trib
    Apr 09, 2009 @ 14:17
    Warren Buckley
    0

    Doing that Richard would cancel the save of the enitre document. I have other properties on the document that I want that type of umbraco user to edit.

    Cheers,
    Warren

  • Warren Buckley 2106 posts 4836 karma points MVP ∞ admin hq c-trib
    Apr 14, 2009 @ 13:06
    Warren Buckley
    0

    This is my current solution that is working that PeterD helped me out with.

    [code]
    void Document_BeforeSave(Document sender, SaveEventArgs e)
    {

    //SETUP string
    string previousGroupNameValue = string.Empty;

    //GET the current user logged into umbraco
    User currentUser = umbraco.helper.GetCurrentUmbracoUser();

    if (currentUser.UserType.Alias == "protoEditor")
    {
    //WE ARE THE protoEditor type

    //IF the node we are editing is of docType = Network
    if (sender.ContentType.Alias == "Network")
    {

    //GET the node as XML in the umbraco XML Cache doc
    XPathNodeIterator networkXMLNode = umbraco.library.GetXmlNodeById(Convert.ToString(sender.Id));


    //This while example from PeterD - I don't understand it personally ?!?
    while (networkXMLNode.MoveNext())
    {
    if (networkXMLNode.Current.NodeType != System.Xml.XPath.XPathNodeType.Root)
    {
    XmlNode n = ((IHasXmlNode)networkXMLNode.Current).GetNode();
    try
    {
    //SETUP a string to get our value doing an xPath select for the property value
    previousGroupNameValue = n.SelectSingleNode("data [@alias='groupName']").InnerText;
    }
    catch
    {

    }
    }
    }


    //Check IF the groupName property's value has been changed
    if (Convert.ToString(sender.getProperty("groupName").Value) != previousGroupNameValue)
    {
    //Lets set the value back to it's original value
    sender.getProperty("groupName").Value = previousGroupNameValue;
    }
    }

    }
    }
    [/code]

    I could do with some help to get a message bubble to appear to notify the user that the value has been changed back.

    Warren

  • keilo 568 posts 1023 karma points
    Apr 24, 2017 @ 02:43
    keilo
    1

    Hi Warren

    I stumble on this old post while looking for how to check if a field's value changed by getting old value and comparing to new (saving) value.

    Im using 7.5.9 and wondering if there are more clear and neat way of doing this check (old value before save and new value while saving).

    Did you figure out a better way of doing this check?

    The bubble notification would be great as well since in my use case I would like to initiate an email when the checkbox field changed from ON to OFF or vice versa - and give the editor the indication an email notification is dispatched.

    Would greatly appreciate your input!

  • Tiago 12 posts 94 karma points
    Oct 13, 2017 @ 11:33
    Tiago
    0

    Hello,

    I was trying to figure out a way to comapre property values of a Member to check if a given property had been changed. I tried multiple ways, not including the one above because I thought that traversing xml nodes had to be my last resort.

    The way I managed to comapre values was by hooking into the MemberServcie.Saving event.

    When the event was fired I found that I had access to both the current Member state (args.SavedEntities) and the 'previous Member state (via the MemberService).

    MembershipHelper memberHelper = new MembershipHelper(UmbracoContext.Current);
    
      foreach(var member in args.SavedEntities)
      {
        bool currSubState = member.GetValue<bool>("<the_property_alias_to_compare>");
    
        var prevMemberState = memberHelper.GetByEmail(member.Email);
    
        LogHelper.Info(typeof(MemberEventHandler), "no prev member state? " + (prevMemberState == null));
        LogHelper.Info(typeof(MemberEventHandler), String.Format("Previous Subscription State: {0}", prevMemberState != null && prevMemberState.GetPropertyValue<bool>("<the_property_alias_to_compare>")));
        LogHelper.Info(typeof(MemberEventHandler), String.Format("New Subscription State: {0}", currSubState));
      }
    

    We must be careful because if we do not do a null check or check if the member is new ( ((IRememberBeingDirty)member).IsDirty() ), we will get a null reference exception when accessing the 'prevMemberState' as it does not exist!

Please Sign in or register to post replies

Write your reply to:

Draft