Copied to clipboard

Flag this post as spam?

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


  • Matthieu Nelmes 102 posts 385 karma points
    Sep 17, 2015 @ 12:09
    Matthieu Nelmes
    0

    Setting Property Values Programmatically

    I have a DocType for a venue that contains address details.

    When a CMS user saves a new store, I want to take the entered postcode, geocode the information to a Lat/Lng and then save the results within a read-only property for the venue.

    My first question is, am I correct in thinking this can be achieved through a custom event handler that hooks into the Publish events such as:

     public class EventHandler : ApplicationEventHandler
    {
        protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            ContentService.Publishing += ContentService_Publishing;
        }
    
        void ContentService_Publishing(IPublishingStrategy sender, PublishEventArgs<IContent> e)
        {
            foreach (var content in e.PublishedEntities)
            {
                if (content.ContentType.Alias == Constants.DocumentTypeAliases.VENUE)
                { 
                var venueInputPostcode = content.GetValue<string>("venuePostcode");
                GeoLocation venueGeoCodedLocation = GeoCoder.GetCoordinates(venueInputPostcode);
                content.SetValue("venueLat", venueGeoCodedLocation.Lat);
                content.SetValue("venueLng", venueGeoCodedLocation.Lng);
                }
            }
        }
    }
    

    My second question is, since the Lat/Lng values are read-only from an end users perspective, is there any way these can be hidden properties within Umbraco?

  • Matthieu Nelmes 102 posts 385 karma points
    Sep 17, 2015 @ 17:30
    Matthieu Nelmes
    101

    Whoa! Hold up.

    I found a potential solution. Instead of hooking into the ContentService.Publishing event I went for ContentService.Published. This way I have access to the NodeId so my solution was as follows:

    Note! This has the potential to create an infinite loop if you're not careful. Because I only want to set the value the first time the postcode is entered, I'm checking to see if the GeoCoded Lat has already been entered.

     public class RegisterEvents : ApplicationEventHandler
    {
        protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            ContentService.Published += ContentService_Publishing;
        }
    
        void ContentService_Publishing(IPublishingStrategy sender, PublishEventArgs<IContent> e)
        {
            foreach (var content in e.PublishedEntities)
            {
                if (content.ContentType.Alias == Constants.DocumentTypeAliases.VENUE)
                {
                    var venueInputPostcode = content.GetValue<string>("venuePostcode");
                    if (content.GetValue<string>("venueLat") == string.Empty)
                    {
                        GeoLocation venueGeoCodedLocation = GeoCoder.GetCoordinates(venueInputPostcode);
                        var contentService = new Umbraco.Core.Services.ContentService();
                        var venueDoc = contentService.GetById(content.Id);
                        venueDoc.SetValue("venueLat", venueGeoCodedLocation.Lat.ToString());
                        venueDoc.SetValue("venueLng", venueGeoCodedLocation.Lng.ToString());
                        contentService.SaveAndPublishWithStatus(venueDoc);
                    }
    
                }
            }
        }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft