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.
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.
- Class Program
- {
- static void Main(string[] args)
- {
- SettingBingMapsKey();
- Console.WriteLine("Bing Maps has been set successfully");
- }
- static private void SettingBingMapsKey()
- {
- ClientContext Mycontext = new ClientContext("<Your Site Url>");
- Web Myweb = Mycontext.Web;
- Myweb.AllProperties["BING_MAPS_KEY"] = "<Bing Maps Key>";
- Myweb.Update();
- Mycontext.ExecuteQuery();
- }
- }
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.
- class Program
- {
- static void Main(string[] args)
- {
- AddingGeolocationField();
- Console.WriteLine("Location field has been added successfully");
- }
- private static void AddingGeolocationField()
- {
- ClientContext Mycontext = new ClientContext("<Site Url>");
- List MyList = Mycontext.Web.Lists.GetByTitle("UNESCO Heritage Sites");
- MyList.Fields.AddFieldAsXml("<Field Type='Geolocation' DisplayName='Location'/>", true, AddFieldOptions.AddToAllContentTypes);
- MyList.Update();
- Mycontext.ExecuteQuery();
- }
- }
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.
14. The added entry will be available in the list as shown below.
15. On clicking the location icon, we can view the actual location on the 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.
17. Both list entries are visible as Pushpins on the Bing Map.