CUBE4

SharePoint Blog

Geo location Field in SharePoint 2013

Last year I wrote a series of articles on a mash up between a SharePoint list and Bing Maps, it was a three part article and took quite a lot of time to get the end result.

Thankfully in SharePoint 2013 Preview, Microsoft has added the functionality in to achieve this fairly easily. This is achieved with a Geo location field. This field stores location information i.e. Longitude/Latitude which can then be rendered on a Bing/Nokia Map. The one downside is that you’re going to need to crack out Visual Studio 2012 in order to add this field, or at least this is what the examples say on MSDN. Hopefully this is added in to the interface by release, I couldn’t find it anywhere however.

Before you get started sort out the following pre-reqs:

  1. SharePoint 2013 Preview Farm
  2. Visual Studio 2012
  3. Bing Maps API Key Available Here

The first stop is to register the Bing Maps Key against SharePoint; you can achieve this using PowerShell with the following command:

Set-SPBingMapsKey –BingKey “

You can also do this via code see how here. This will make it easy if you’re packaging up an application that takes advantage of these features.

The next thing to do is to add a new custom list, in this example I have created one called “Office Locations”. In this example I have renamed “Title” to “Name” and added an “About” column of type multi-lines of text.

When you’re done the list should look similar to the following:

Open up Visual Studio 2012 and create a new Console Application, in this example I have just given it a name of AddGeoField.

The first thing we need to do is add assembly references to the two following dll’s:

  • Microsoft.SharePoint.Client.dll 
  • Microsoft.SharePoint.Client.Runtime.dll 

Open the class (.cs) file and add a using statement at the top of the file:

 c# |  copy code |? 
1
using Microsoft.SharePoint.Client;

Now we need to add the code to the console application I have made this generic to read in the arguments, this way we can reuse the same console application anywhere in our farm.

 c# |  copy code |? 
01
class Program
02
    {
03
        static void Main(string[] args)
04
        {
05
            AddGeolocationField(args);
06
            Console.WriteLine("Location field added successfully");
07
        }
08
        private static void AddGeolocationField(string[] args)
09
        {
10
            // Replace site URL and List Title with Valid values.
11
            ClientContext context = new ClientContext(args[0]);
12
            List oList = context.Web.Lists.GetByTitle(args[1]);
13
            oList.Fields.AddFieldAsXml(<Field Type='Geolocation' DisplayName='Location'/>, true, AddFieldOptions.DefaultValue);
14
            oList.Update();
15
            context.ExecuteQuery();
16
        } 
17
18
    }

Build the solution and then run the application from the command line, replace the arguments with ones relevant to your environment. Make sure the account your running under has sufficient permissions to access the site collection/web/list.

AddGeoField.exe http://sp2013 “Office Locations”

Now if we go back to our list, we can see the Location column has been added of type Geolocation.

When adding a new item, we can either use the current location which will be useful for devices with GPS built in, or add the longitude/latitude of the current location.

When you click specify location, a second modal popup opens where you can specify the values required:

Once valid values have been entered, a Bing map preview is displayed to give you a indication that you have the co-ordinates correct.

After adding a few office locations, the list will look similar to the following:

You can view the Bing Map in various ways:

  1. Opening a list item:

  2. Clicking on the icon in the geo location field, this is a great way for a fast preview:

Lastly, you can also now create a map view, if you create a new view, you will see a Map View type.

When I was creating my view I got an error, but the view created anyway, when you switch to it you get a awesome representation of all the list items.

I think you would agree this is a lot easier than what we had to do in previous versions of SharePoint. I would love to see uses you can come up with when you put this in to practice.

If you want to save some time I have uploaded my compiled solution for you to use: Add Geo Location (361)

About Chris Grist

Chris SharePoint Architect for Beach Energy in Adelaide, South Australia. Chris has specialised in SharePoint for the last few years and has over 12 years experience in the IT Industry. Catch Chris on twitter at @griStdoG View all posts by Chris Grist →
This entry was posted in SharePoint, SharePoint Development and tagged , , , . Bookmark the permalink.

4 Responses to Geo location Field in SharePoint 2013

  1. Totally awsome, you’ve just given me something to try out over the weekend! It seems strange that we have to go through this in order to surface a new column type. Hopefully we can do this via the UI in the actual product release version.

  2. Chris Grist

    Thanks, ive fixed the code in to proper blocks so should be a bit easier to copy/paste now.

  3. Chris Grist

    should be “” when i added the ” it disappears from the post

Leave a Comment

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>