Copied to clipboard

Flag this post as spam?

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


  • Tom 713 posts 954 karma points
    Sep 23, 2013 @ 03:58
    Tom
    0

    Save/Publish Events Not Updating Values

    I'm using the ContentService but am not sure where I can hook in to update a property value and have it save.. I'm doing the following:

     

    public class RegisterCustomEvents : ApplicationEventHandler
        {
            protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
    
                base.ApplicationStarted(umbracoApplication, applicationContext);
                ContentService.Saving += ContentService_Saving;
            }
    
            void ContentService_Saving(IContentService sender, Umbraco.Core.Events.SaveEventArgs<IContent> e)
            {
                GeolocateStockists(sender, e.SavedEntities);
            }
    
            void GeolocateStockists(IContentService contentService, IEnumerable<IContent> savedEntities)
            {
                if (savedEntities != null)
                {
                    var stockists = savedEntities.Where(s => s.ContentType.Alias == StockistDocTypeAlias);
                    if (stockists.Any())
                    {
                        foreach (var stockist in stockists)
                        {
                            var addressProperty = stockist.Properties[StockistAddressPropertyName];
                            var address = addressProperty != null ? addressProperty.Value.ToString() : "";
    
                            if (!string.IsNullOrWhiteSpace(address))
                            {
                                var geoResult = GoogleGeoCoder.GetLocationFromGoogleMaps(address);
    
                                if (geoResult.Results.Any())
                                {
                                    stockist.SetValue(StockistLatLongPropertyName, geoResult.Results[0].Geometry.Location.Lat.ToString(Utility.NumberFormatInfo) + "," + geoResult.Results[0].Geometry.Location.Lng.ToString(Utility.NumberFormatInfo) + ",13");
                                }
                            }
                        }
                    }
                }
            }
    
            const string StockistDocTypeAlias = "Stockist";
            const string StockistAddressPropertyName = "stockistAddress";
            const string StockistLatLongPropertyName = "latLong";
        }

    Unfortunately the new value isn't saved to the content item. Just wondering what I'm missing

    I tried caling contentService.Save(stockist) without success after the call to SetValue

    Thanks,

    Tom

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Sep 23, 2013 @ 12:12
Please Sign in or register to post replies

Write your reply to:

Draft