+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Address field in custom module

Address field in custom module

9 posts, 1 answered
  1. Rico
    Rico avatar
    88 posts
    Registered:
    30 Aug 2012
    24 May 2013
    Link to this post
    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
  2. Rico
    Rico avatar
    88 posts
    Registered:
    30 Aug 2012
    24 May 2013 in reply to Rico
    Link to this post
    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);
    Answered
  3. Mark
    Mark avatar
    147 posts
    Registered:
    01 Jun 2010
    24 May 2013
    Link to this post
    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?
  4. Rico
    Rico avatar
    88 posts
    Registered:
    30 Aug 2012
    24 May 2013 in reply to Mark
    Link to this post
    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?
  5. Arno
    Arno avatar
    249 posts
    Registered:
    08 Sep 2010
    10 Jun 2013
    Link to this post
    Let's bump this one as I am interested as well.
  6. Stoimen Stoimenov
    Stoimen Stoimenov avatar
    60 posts
    Registered:
    11 Jul 2016
    10 Jun 2013
    Link to this post
    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
  7. Arno
    Arno avatar
    249 posts
    Registered:
    08 Sep 2010
    12 Jun 2013 in reply to Stoimen Stoimenov
    Link to this post
    Thanks for that Stoimen!
  8. Mark
    Mark avatar
    147 posts
    Registered:
    01 Jun 2010
    25 Jul 2013
    Link to this post
    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)??
  9. Petya
    Petya avatar
    123 posts
    Registered:
    17 Oct 2016
    30 Jul 2013
    Link to this post
    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
9 posts, 1 answered