Copied to clipboard

Flag this post as spam?

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


  • Rob Watkins 369 posts 701 karma points
    Jan 29, 2013 @ 11:09
    Rob Watkins
    0

    Auto-update member properties on save

    I have a member type with a postcode field.

    Using the ApplicationBase, I've set up an AfterSave handler to geocode this and update the member with the easting and northing.

    I then try to use this in searches.

    The code I am using is this:

    if (geo.Success)
                        {
                            Property peasting = member.getProperty("easting");
                            Property pnorthing = member.getProperty("northing");
    
                            try
                            {
                                peasting.Value = geo.Easting;
                                pnorthing.Value = geo.Northing;
    
                                msg = "Geocoded member " + member.LoginName + ", postcode " + postcode + ", easting " + geo.Easting.ToString() + ", northing " + geo.Northing.ToString();
                            }
                            catch (Exception e)
                            {
                                msg = "Geocode save failure for member " + member.LoginName + ", postcode " + postcode + ", error " + e.Message;
                            }
                        }

    This appears to work from the member editing side - if you reload the member, the easting and northing is there. However, when I retrieve the published XML for searching (I'm using uComponents for this):

    XmlDocument centres = uComponents.Core.uQuery.GetPublishedXml(uComponents.Core.uQuery.UmbracoObjectType.Member);

    ...the easting and northing properties are blank; they remain that way until I save the member a second time in the admin system, presumably because my property changes are not published.

    I can't just call Save() after setting the property values because that will go into an infinite loop; is there anything else I can call to publish this that will not fire the AfterSave() event again? 

  • Rob Watkins 369 posts 701 karma points
    Jan 29, 2013 @ 11:10
    Rob Watkins
    0

    As an addendum, I also can't just click the save button twice; I have to save the postcode change, reload the member from the tree to get the new easting / northing, and then save that.

  • Rob Watkins 369 posts 701 karma points
    Jan 29, 2013 @ 11:36
    Rob Watkins
    0

    Actually, I've just very easily solved this by simply using exactly the same code on the BeforeSave event handler rather than AfterSave. No idea why I didn't hink of this before.

  • Rob Watkins 369 posts 701 karma points
    Jan 29, 2013 @ 12:44
    Rob Watkins
    0

    As an addendum to the addendum, because this may help someone else, once I had fixed this problem, it became necessary to republish all the existing members to ensure that the easting / northings were updated in the member XML for any affected members, and it's important to note that Content -> right-click -> Republish All does NOT do this. 

    I solved it by writing a very simple user control to go through all members executing Save() and adding that to the member dashboard.

    The important code is very simple (not sure if you even need the error catching as I didn't bother to check what Umbraco does on Save(), but it's there just in case):

    List errors = new List();
    
    umbraco.cms.businesslogic.member.Member[] members = umbraco.cms.businesslogic.member.Member.GetAll;
    
    foreach (umbraco.cms.businesslogic.member.Member m in members)
    {
            try
            {
                    m.Save();
            }
            catch
            {
                    errors.Add(m.LoginName);
            }
    }
    

    Whack that on a button click handler and you have a republish button for members. It does take a while though, even for a couple of hundred members, although I do have webservice geocode lookups on every single save, so it may just be my use case that makes it particularly bad.

    EDIT: Altered loop to use local variable as per http://our.umbraco.org/forum/developers/api-questions/7875-How-to-loop-through-all-members#comment29258

Please Sign in or register to post replies

Write your reply to:

Draft