Address field in custom module
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
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);
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?
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?
Let's bump this one as I am interested as well.
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();
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();
using
Telerik.Sitefinity.GeoLocations.Model;
Thanks for that Stoimen!
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)??
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.