Address field in custom module

Posted by Community Admin on 04-Aug-2018 16:01

Address field in custom module

All Replies

Posted by Community Admin on 24-May-2013 00:00

Hello,

I'm trying to use the new 'Address' field for custom content items. After adding in this field, and going to the 'code reference' section of the module -- the example code does not contain any reference to this field. What is the proper way to access this field via the API?
My objective is I have to import data from a separate system into sitefinity, and am trying to pre-populate all the address information (address, city, zip code, state, country, etc) into this 'Address' field -- however I'm not sure how to specifically reference each attribute of this new field. I've also looked at the online documentation -- but again can't find any information to do this by means of the api...

I see that there is a 'GetAddressFields()' method on the actual DynamicContent item -- however there is no equivalent 'setter' method...


I'd appreciate some insight please...

Thanks

Posted by Community Admin on 24-May-2013 00:00

Found the answer on my own :)

Telerik.Sitefinity.GeoLocations.Model.Address addr = new Address();
addr.City = "blah";
addr.Street = "blah";
etc...

<dynamicItem>.SetValue("Address", addr);

Posted by Community Admin on 24-May-2013 00:00

After you've populated the Address, City, State, Zip, how can you use Sitefinity to get the Lat/Lon as you can in the backend?

Posted by Community Admin on 24-May-2013 00:00

Great question -- I've been trying to figure that as well. I can populate all the fields other than the lat/lon. It appears these are populated via client side script (using Google API I'm assuming -- which does make sense)... Sitefinity? Is there a way to get around this somehow? Call a web service or something via the backend code?

Posted by Community Admin on 10-Jun-2013 00:00

Let's bump this one as I am interested as well.

Posted by Community Admin on 10-Jun-2013 00:00

Hi,

Here is an example how to set all of the properties of an address field:

DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager();
Type sightseeingType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.Tourism.Sightseeing");
Guid itemId = new Guid("1d708efa-4f3c-4e8a-85dc-f72ff3f9f1c3");
DynamicContent sightseeingItem = dynamicModuleManager.CreateDataItem(sightseeingType, itemId, dynamicModuleManager.Provider.ApplicationName);
 
//Creating address object that is going to be assigned to the address field
Address address = new Address();
address.Longitude = -118.32666;
address.Latitude = 34.10163;
address.CountryCode = "US";
address.StateCode = "CA";
address.City = "Los Angeles";
address.Street = "Hollywood Boulevard";
address.Zip = "90028";
 
sightseeingItem.SetValue("Location", address);
 
sightseeingItem.SetValue("Name""Best of Los Angeles");
sightseeingItem.SetString("UrlName""best-of-los-angeles");
sightseeingItem.SetValue("Owner", SecurityManager.GetCurrentUserId());
sightseeingItem.SetValue("PublicationDate", DateTime.Now);
 
// We can now call the following to publish the item
ILifecycleDataItem publishedSomeContentItem = dynamicModuleManager.Lifecycle.Publish(sightseeingItem);
 
//You need to set appropriate workflow status
sightseeingItem.SetWorkflowStatus(dynamicModuleManager.Provider.ApplicationName,"Published");
 
// You need to call SaveChanges() in order for the items to be actually persisted to data store
dynamicModuleManager.SaveChanges();

And here is an example how to get the address field and access its properties: 

DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager();
Type sightseeingType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.Tourism.Sightseeing");
 
DynamicContent item = dynamicModuleManager.GetDataItem(sightseeingType, itemId);
 
// Then we check it out
DynamicContent checkOutSightseeingItem = dynamicModuleManager.Lifecycle.CheckOut(item) as DynamicContent;
 
var address = checkOutSightseeingItem.GetValue<Address>("Location");
 
address.Street = "Hooper Ave";
var lon = address.Longitude;
var lat = address.Latitude;
address.Latitude = 38.10163;
address.Longitude = -108.32666;
         
// We can now change any values of the item
checkOutSightseeingItem.SetValue("Location", address);
 
// Now we need to check in, so the changes apply
ILifecycleDataItem checkInSightseeingItem = dynamicModuleManager.Lifecycle.CheckIn(checkOutSightseeingItem);
 
//Finnaly we publish the item again
dynamicModuleManager.Lifecycle.Publish(checkInSightseeingItem);
 
// You need to call SaveChanges() in order for the items to be actually persisted to data store
dynamicModuleManager.SaveChanges();

For the Address object the following namespace have to be added:

using Telerik.Sitefinity.GeoLocations.Model;

A documentation article describing how to work with the address field from code will be prepared and published soon.

If you have another questions don't hesitate to contact us

Regards,
Stoimen Stoimenov
Telerik
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 12-Jun-2013 00:00

Thanks for that Stoimen!

Posted by Community Admin on 25-Jul-2013 00:00

Stoimen, how can I use the new GeoLocation API to get the Lat/Lon when I have all of the other location information (city, state, address, zip)??

Posted by Community Admin on 30-Jul-2013 00:00

Hello Mark,

I am afraid there is no direct way to retrieve the coordinates of a city/address via the GeoLocation API. You can read more about its usage in our documentation here.

The Address field is using the Google Maps v3 API for retrieving coordinates when auto navigating to a Street or Town in a particular country. I am not sure if this is what you want to achieve. Could you provide some additional information? 

Thanks in advance. 

Regards,
Petya
Telerik
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

This thread is closed