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:
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?
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);
}
}
}
}
}
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:
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?
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.
is working on a reply...