SharePoint 2013. Geolocation field type

In SharePoint 2013 Preview there is a new field type named Geolocation (Geographic location that represents a point on a map) that enables you to use location data. Also there is a new type of list view - Map View that represents list data on map.
Bing Maps service is used for showing data in that case. In standard list view type callouts are used for location data:

SharePoint Geolocation Field Type

SharePoint Geolocation Field Type

And this is a map view of list data in SharePoint 2013 Preview:

SharePoint 2013 Map View

SharePoint 2013 Map View

Not for end users

By default Geolocation column is not available for users in SharePoint 2013 Preview. And the reason is this: there is a geolocation field type but not any field using it in SharePoint 2013. There is the article in MSDN about adding geolocation column to a list programmatically using SharePoint client object model. But this solution is ugly at least. So we need another one.

Geolocation field solution

To implement support for Geolocation field type in SharePoint 2013 Preview both for end users (in UI) and for developers (in Visual Studio 2012) is enough to create a tiny sandbox solution which will contain only one element - geolocation site column. The whole solution is placed in ten rows:

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">  
  <Field
        ID="{43db0e75-7489-4e6e-89d2-14c6cf26252f}"
        Name="GeolocationColumn"
        DisplayName="Geolocation Column"
        Type="Geolocation"
        Group="Geolocation">
  </Field>
</Elements>

By deploying this solution in SharePoint we are able to add a Geolocation column in any list or document library:

Adding geolocation column

Adding geolocation column

And it will be available in the content type design in Visual Studio 2012:

Adding geolocation column to content type in Visual Studio 2012

Adding geolocation column to content type in Visual Studio 2012

One tiny sanbox solution, which be deployed even remote (this is a sandbox solution) and no any pain in the ass (using console application to add a column to a list).

Inside Geolocation

In the SharePoint 2013 object model Geolocation field represented by SPFieldGeolocation class, which directly derived from SPField, i.e. no any dependencies on other field types:

Geolocation Field Type

Geolocation Field Type

There are only two columns of type geography in dbo.AllUserData table to store the values. So adding even third column of this type to the list data will take up to two lines on each item in the list, which will reduce performance. I hope there will be more columns in this table for storing geolocation data.
dbo.AllUserData table now is wide table and contains more then 4K columns! So a couple of additional fields for geolocation data is nothing.

Working with geolocation data

Now a few examples of using this new data type in your solutions. In SharePoint 2013 was represented a new class to work with geolocation data. It's SPFieldGeolocationValue:

var web = SPContext.Current.Web;
var list = web.GetList("GeoLocationDemoList");
var item = list.GetItemById(1);
var geoVal = item["GeoField"] as SPFieldGeolocationValue;
if (geoVal != null)
{
    var lat = geoVal.Latitude;
    var lon = geoVal.Longitude;
    var mes = geoVal.Measure;
    var alt = geoVal.Altitude;
}

Here is an easy way to set the values of latitude, longitude, measure and altitude. SPFieldGeolocationValue class has a full set of constructors for creating new fild values:

public SPFieldGeolocationValue();
public SPFieldGeolocationValue(string fieldValue);
public SPFieldGeolocationValue(double latitude, double longitude);
public SPFieldGeolocationValue(double latitude, double longitude, 
                               double altitude, double measure);

fieldValue has the following format:

POINT ({Latitude}, {Longitude} [{Measure}, {Altitude}])

In the same format you can get the value by calling ToString method of SPFieldGeolocationValue object.

Geolocation and Linq to SharePoint

Linq to SharePoint since SharePoint 2013 can work with columns of geolocation type. This is the only new feature in Linq to SharePoint 2013 :(. SPMetal generates code for geolocation columns like this:

[Microsoft.SharePoint.Linq.ColumnAttribute(Name = "Geolocation", Storage = "_geo", FieldType = "Geolocation")]
public string Geo
{
    get
    {
        return this._geo;
    }
    set
    {
        if ((value != this._geo))
        {
            this.OnPropertyChanging("Geo", this._geo);
            this._geo = value;
            this.OnPropertyChanged("Geo");
        }
    }
}
private string _geo;

I.e. data will be represented in plain text in described above format. For working with data you can incapsulate in your class funcationallity of SPFieldGeolocationValue class.

But there is another way. Geography data type also described in Microsoft.SqlServer.Types.dll assembly, which is a part of SQL Server. In my case it is located at C:\Program Files (x86)\Microsoft SQL Server\110\SDK\Assemblies folder. Class for working with geography type data is SqlGeography and it has constructor which takes geolocation data in plain text format:

var Geography = SqlGeography.Parse(item.Geo);
var lat = Geography.Lat;
var lon = Geography.Long;

This class can be usefull for implementing complex logic is case working with geolocation data. Finnaly I'd say geolocation data filtering and sorting are not suppurted by SharePoint 2013. But I think there are options to get around this limitation.

Vitaly Zhukov

Vitaly Zhukov

SharePoint Architect, Developer, Technical Trainer, Microsoft MVP (Office Development). Above 15 years of total experience working with SharePoint, Dynamics CRM, Office 365, and Microsoft stack development.

You May Also Like

Collect SharePoint telemetry with Azure Application Insights. Part I. Server-Side

Collect SharePoint telemetry with Azure Application Insights. Part I. Server-Side

SharePoint New Team Site. Inside Out

SharePoint New Team Site. Inside Out

SharePoint 2019 Preview

SharePoint 2019 Preview

SharePoint Ribbon. Creating multi level menu

SharePoint Ribbon. Creating multi level menu

Mask "Created By" and "Modified By" user names from forms and views

Mask "Created By" and "Modified By" user names from forms and views

SharePoint list item attachments' size

SharePoint list item attachments' size

SharePoint 2010. Custom forms for ContentType

SharePoint 2010. Custom forms for ContentType

SharePoint 2007. The max/min value of filed in SharePoint list

SharePoint 2007. The max/min value of filed in SharePoint list