How to add GeoLocation fields to SharePoint 2013 Site

SharePoint 2013 has one of the new capability that supports GeoLocation fields. The GeoLocation field helps us to build location- aware SharePoint 2013 applications. To keep it simple, SharePoint 2013 lists support a new column of type ‘Location’. where we can set or retrieve the latitude and longitude co-ordinates of a place in decimal. By default this Location column is not available to us.

The pre-requisite to enable this Location column is to install an msi package called SQLSysClrTypes.msi on every front-end server of SharePoint 2013 Farm. The SQLSysClrTypes.msi installer is available in the Microsoft SQL 2008 R2 SP1 Feature Pack

http://www.microsoft.com/en-us/download/details.aspx?id=26728

This installer is responsible for implementing geometry, geography and hierarchy ID types in SQL Server 2008. I’m going to use my Office 365 dev tenant for this demonstration, where this installer is already installed (by default available in SP Online).There are multiple ways of adding the Location columns. Here i would be leveraging the SharePoint 2013 Client Object Model to create location columns.

Uri oUri = new Uri("https://yourtenant.sharepoint.com");

MsOnlineClaimsHelper claimsHelper = new MsOnlineClaimsHelper(oUri, "yourid@yourtenant.onmicrosoft.com", "yourpassword");


using (ClientContext context = new ClientContext(oUri))
   {
     context.ExecutingWebRequest += claimsHelper.clientContext_ExecutingWebRequest;

     List oList = context.Web.Lists.GetByTitle("TestList");
     oList.Fields.AddFieldAsXml("<Field Type='Geolocation' DisplayName='VistingLocation'/>", true,
                                    AddFieldOptions.AddToAllContentTypes);
     oList.Update();
     context.ExecuteQuery();
                    

    }
when the above code is executed it creates the location column in the custom list.
pic2

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

I tried to create more than 2 location columns in the list, it threw an error like “There are too many columns of the specified data type. Please delete some other columns first. Note that some column types like numbers and currency use the same data type.”. I’ve learned that at this point of time, the a SP 2013 lists does not support more than 2 Location columns.

The next step is to add the desired locations to the list items. For the sake of demo, i will add the locations manually.

lat lon

 

Basically you need to know the latitude and longitude of a place to be added to the GeoLocation field.

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

It does not give a good user experience to view the locations in the latitude and longitude format. SharePoint 2013 provides map- view functionality of Lists which has GeoLocation fields associated with it. To enable MapView, the Microsoft Bing Map keys need to be registered with the respective SharePoint 2013 tenant. Here is the code snippet for registering Bing Map key with SharePoint 2013 instance.

Uri oUri = new Uri("yoursharepointurl");

 MsOnlineClaimsHelper claimsHelper = new MsOnlineClaimsHelper(oUri, "yourid", "yourpassword");

   using (ClientContext context = new ClientContext(oUri))
            {
                context.ExecutingWebRequest += claimsHelper.clientContext_ExecutingWebRequest;
                 Web web = context.Web;
                 web.AllProperties["BING_MAPS_KEY"] = "ArJrGcrUw_rcR65nsyG7Ahm1VkhOOt6h0eIdd7ai_g0ykyJsIPSfkfwmW_MwcPdr";
                web.Update();
                context.ExecuteQuery();
                              
            }
Create View –> Map View
Create View[4]
CreateView1
 

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

MapView1

Now you can see the location field data rendered in the Map View format.

 Subscribe to my blog

2 thoughts on “How to add GeoLocation fields to SharePoint 2013 Site

  1. Pingback: How to add GeoLocation fields to SharePoint 2013 Site - My experiments with SharePoint, Azure and .NET using Visual Studio

  2. Thanks for the info. I am not able to see GeoLocation type columns in the site columns. I am using office 365 MS online trial version. Is it enabled by default for office 365 online? If not then how we can do that. Thanks.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

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

Facebook photo

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

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.