Working with Geo Location Fields in SharePoint 2013

One of the new features in SharePoint 2013 is the new Geo Location field Type.Using this new capability, you can display locations by specifying the latitude and longitude of the location , also display the location in Bing maps (All these Out of the box !) . See below the screenshot of the Geo location column in OOTB forms.

Add/Edit Forms: You can specify the latitude and longitude of the location.

add_edit

View Form: The location will be displayed in Map.

view

The OOTB SharePoint Geo location field type is integrated with Bing Map to provide the Map functionality.In most of the applications this is sufficient. but in case if you want to configure Geo Location column to use any other Map provider such as Google Maps, Nokia Here Maps etc you can create a custom field type which is integrated with your preferred map provider. You can read this Blog on Creating SharePoint 2013 GeoLocation field using Google Maps.

In this post, you can see the steps required to create a Geo Location column using Bing Maps in any of your site.

Step 1: Obtain a Bing Maps Key

First of all, you will have to register to Bing Maps Portal. After you register and sign in to the portal, you can create a new Key by Selecting Create or View Keys under My Account.

createkey

The Following information should be entered while you register for a Bing Maps key:

  • Application name: This is the name of the application which will be using this Key.
  • Application URL : The URL of the application.
  • Key type : The key type is an important information. Before you generate a key you should analyse your requirements and find the most suited Key type for your application. There are 2 Key Types: Basic Key and Enterprise Key.
  • Application type : This is a choice field , you have to select the option that fits close to the type of application which will be using the Key.

The Basic Key is a free key for limited use and Enterprise Key is consumer/commercial applications. Read this Post for  information about the application type and for Deciding between Basic and Enterprise Bing Map Key .

Step 2: Register the Bing Maps Key to your SharePoint Site/Farm

The Bing Maps key you have generated will be use to authenticate your application while consuming Bing Maps APIs. For this you need to register the Bing Maps key at Web or Farm level depending on your requirements.

To register key at Farm level run the following command in SharePoint 2013 Management Shell

Set-SPBingMapsKey –BingKey “<Your Bing Maps key>”

Registering the Key in farm is beneficial when you have large number of sites which will be using the Geo Location features.

To register key at web level you have to run the following code in a console application or Open the web in designer and Add a new web property.

Using Designer

Open the web in designer–> Site Options and add new parameter as below.

siteproperty

 Using Console application

Make sure following dlls are added to project.

  • Microsoft.SharePoint.Client.dll
  • Microsoft.SharePoint.Client.Runtime.dll
class Program
    {
        static void Main(string[] args)
        {
            SetBingMapsKey();
            Console.WriteLine("Bing Maps set successfully");
        }
     static private void SetBingMapsKey()
        {

            ClientContext context = new ClientContext("<Site Url>");
            Web web = context.Web;
            web.AllProperties["BING_MAPS_KEY"] = "<Valid Bing Maps Key>"
            web.Update();
            context.ExecuteQuery();
        }    
    }

Important: The key registered at the web have precedence over the farm level key.

Step 3: Create Geo Location column in List

The Geo Location field type is by default hidden from the SharePoint UI. So there is no way to create a Geo Location column from SharePoint UI. So Geo Location columns needs to be created using custom code.

Create Geo Location column in a List using C# CSOM

 Make sure following dlls are added to project.

  • Microsoft.SharePoint.Client.dll
  • Microsoft.SharePoint.Client.Runtime.dll
        private static void AddGeolocationField()
        { 
         // Replace site URL and List Title with Valid values.
            ClientContext context = new ClientContext("<Site Url>"); 
            List oList = context.Web.Lists.GetByTitle("<List Title>");
            oList.Fields.AddFieldAsXml("<Field Type='Geolocation' DisplayName='<ColumnName>'/>",true, AddFieldOptions.AddToAllContentTypes);                                        
            oList.Update();
            context.ExecuteQuery();
        } 

Create Geo Location column in a List using JSOM

function AddGeolocationField(url,listName,fieldName)
{
var clientContext = new SP.ClientContext(url);
var targetList = clientContext.get_web().get_lists().getByTitle(listName);
fields = targetList.get_fields();
fields.addFieldAsXml("<Field Type='Geolocation' DisplayName='"+fieldName+"'/>",true,SP.AddFieldOptions.addToDefaultContentType);
clientContext.load(fields);
clientContext.load(targetList,'DefaultViewUrl');
clientContext.executeQueryAsync(function(){
alert("Geo Location column created.");

}, function(sender,args){
alert("GeoLocation field creation failed:"+args.get_message() );

});
}

If you want to create a Geo Location Site Column then use below PowerShell code

$weburl = "http://siteurl"

$fieldXml = "<Field Type ='Geolocation' DisplayName='<Column Name>' />"

$web = get-spweb $weburl

$field = $web.Fields.AddFieldAsXml($fieldXml)

$web.Update()

Result:

You can now see the Geo Location column in your list and you can add new items  by specifying the latitude and longitude of the location.

add_edit

and see the location on Bing Map

view

Now you have the Geo location columns and can build application on SharePoint which leverages this.

See this post to know Add/Update items with Geolocation columns  in SharePoint 2013

Other Resources:

Creating Map view in SharePoint 2013

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s