SharePoint 2013. Geolocation field type

Saturday, September 1, 2012

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:

Geolocation field type in SharePoint 2013 Preview

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

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:

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

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

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

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:

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:

  1. var web = SPContext.Current.Web;
  2. var list = web.GetList("GeoLocationDemoList");
  3. var item = list.GetItemById(1);
  4. var geoVal = item["GeoField"as SPFieldGeolocationValue;
  5. if (geoVal != null)
  6. {
  7.     var lat = geoVal.Latitude;
  8.     var lon = geoVal.Longitude;
  9.     var mes = geoVal.Measure;
  10.     var alt = geoVal.Altitude;
  11. }

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:

  1. public SPFieldGeolocationValue();
  2. public SPFieldGeolocationValue(string fieldValue);
  3. public SPFieldGeolocationValue(double latitude, double longitude);
  4. public SPFieldGeolocationValue(double latitude, double longitude, 
  5.                                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:

  1. [Microsoft.SharePoint.Linq.ColumnAttribute(Name = "Geolocation"
  2.                   Storage = "_geo", FieldType = "Geolocation")]
  3. public string Geo
  4. {
  5.     get
  6.     {
  7.         return this._geo;
  8.     }
  9.     set
  10.     {
  11.         if ((value != this._geo))
  12.         {
  13.             this.OnPropertyChanging("Geo"this._geo);
  14.             this._geo = value;
  15.             this.OnPropertyChanged("Geo");
  16.         }
  17.     }
  18. }
  19. 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:

  1. var Geography = SqlGeography.Parse(item.Geo);
  2. var lat = Geography.Lat;
  3. 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.

Share

Comments