Geolocation Field in SharePoint 2013

SharePoint 2013 has introduced a new field type called "Geolocation" that enables SharePoint lists to store location information on a map (powered by Bing Maps). It also introduces a new view type called the “Map View” that displays all list items as pushpins on the Bing Map.

The Geolocation column is not available by default and needs to be added programmatically to the SharePoint list. This involves 3 key steps that are elaborated below.

  • Obtaining a Bing map Key
  • Set the key at either the farm or web level in SharePoint
  • Add the Geolocation column programmatically to the SharePoint list

1. Obtain a Bing Map Key from the Bing Maps Account Centre

  • Sign into the website using your Microsoft account (Window Live Id) details
  • Click on "create or view keys" from the left navigation menu.
  • Enter details and press the Submit button. I generated a trail key for my intranet site with 90 day validity.
  • Save the key details.
key details 
 
2. Once the Bing Map key is available, it needs to be set either at the farm or web level in SharePoint. I set it at web level through the console application utilizing the Client Object Model.

3. Create a console application in Visual Studio using the target Framework 4.0 and add a reference to the following SharePoint Client assemblies:
  • Microsoft.SharePoint.Client.dll
  • Microsoft.SharePoint.Client.Runtime.dll

4. Add the following code in the main method of the .cs file. Replace <Site Url> with the top-level site of the web application and <Bing Maps Key> with the key generated in Step 1.

  1. Class Program  
  2. {  
  3.     static void Main(string[] args)  
  4.     {  
  5.         SettingBingMapsKey();  
  6.         Console.WriteLine("Bing Maps has been set successfully");  
  7.     }  
  8.     static private void SettingBingMapsKey()  
  9.     {  
  10.         ClientContext Mycontext = new ClientContext("<Your Site Url>");  
  11.         Web Myweb = Mycontext.Web;  
  12.         Myweb.AllProperties["BING_MAPS_KEY"] = "<Bing Maps Key>";  
  13.         Myweb.Update();  
  14.         Mycontext.ExecuteQuery();  
  15.     }     
  16. }  
5. Run the code, the key will be set at Web level.

6. The next step is to add a Geolocation field to a SharePoint list programmatically by using the SharePoint client object model.

7. I have created a SharePoint list called "UNESCO Heritage Sites". It has the following 3 columns:

  • Title (Default)
  • Description ( Multiple lines of text)
  • Country ( Single line of text)

8. We will add a Geolocation field to it called "Location".

9. We will use a console application and add a reference to the SharePoint Client assemblies as earlier.

10. Add the following code to the application and replace the Site URL, List Title and display name of the field with your site, list and field details.

  1. class Program  
  2. {  
  3.     static void Main(string[] args)  
  4.     {  
  5.         AddingGeolocationField();  
  6.         Console.WriteLine("Location field has been added successfully");  
  7.     }  
  8.     private static void AddingGeolocationField()  
  9.     {  
  10.         ClientContext Mycontext = new ClientContext("<Site Url>");  
  11.         List MyList = Mycontext.Web.Lists.GetByTitle("UNESCO Heritage Sites");  
  12.         MyList.Fields.AddFieldAsXml("<Field Type='Geolocation' DisplayName='Location'/>"true, AddFieldOptions.AddToAllContentTypes);  
  13.         MyList.Update();  
  14.         Mycontext.ExecuteQuery();  
  15.     }  
  16. }

 

11. Run the code, the field will be added to the list.

12. Add an entry to the list to verify the functionality.

13. The Location field expects the Longitude and Latitude of the location.

 Latitude of the location

14. The added entry will be available in the list as shown below.

added entry

15. On clicking the location icon, we can view the actual location on the Bing Map.

Bing Map

16. Adding the Geolocation field to the list also enables the creation of a “Map View”. To create this view, go to list settings then choose view type then choose Map View then choose required columns.
 
Map View

17. Both list entries are visible as Pushpins on the Bing Map. 
 
Map