Alterian ACM : How To Save Data Back To A Page

We had a requirement to Geo-tag news articles.  This left us with a dilemma… we didn’t want content editors to have to manually add in the latitude and the longitude for each news item, but, the smart client did not provide enough flexibility for me to design something that was nice and automatic in the editor.  I finally decided to use a locations field within the news content type, content editors could manually type in the address and, on page load, I could then use the GoogleMapsAPI from Nuget to automatically find the latitude and longitude.

The biggest problem with this concept is that each time the feed is requested, a call to Google’s Geo-tagging service was made, which made the page load time stupidly slow (you can also get blacklisted by Google if you make too many requests).  To get round this, I wanted to write the latitude and longitude returned from Google into ACM so that the next time the article was requested, I wouldn’t have to request the information from Google again.

In order to write back into ACM I started off trying to use the item.edit() method. What I found after some experimentation, was, if the code threw an error between the edit and the saveItem() the item remained locked.  This meant that the next time the article was requested, it was still locked and the item.edit() method caused an Exception, meaning the lat/long was never stored.  If you find yourself in a similar situation, what I suggest you use is the item.ForceEditState. This will set the item into edit mode and never throw exception based on its current state, so you don’t have to worry that your code might break, if, for example a content editor is editing the item while someone was trying to view the map.

If this sounds like something you may need, then my code snippet is below should help. Enjoy!

location = CallGeoTaggingService(address);
if (location != null)
{
var dp = new DataProvider(ReturnFields.All);
var itemToEdit = dp.GetItem(itemUrl);
if (itemToEdit.Editable)
{
itemToEdit.ForceEditState();
itemToEdit.setFieldValue("latitude", location.Latitude);
itemToEdit.setFieldValue("longitude", location.Longitude);
itemToEdit.saveChanges();
}
}

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

More Posts