Saturday, October 13, 2012

Geolocation field in SharePoint 2013

Today I have played with this field and really fantastic to have this feature in SharePoint. It's going to be very useful in our applications.

I have wasted around 30 minutes of time to find where is the Geolocation field to manually add from browser. I searched in the Site Columns list and didn't find it. Thought I have to activate any features and searched Site collection features and site features.... But no luck.

After did some research and found that we have to add the Geolocation field through programming. Is it? really? Why? So many questions and started doing research more time and everyone given the examples in C# (Server side coding) to add Geolocation field to a list. OK. now what? Below is what I have did to check the Geolocation functionality.
Things you have to know:
  • The Geolocation field is available in SharePoint 2013 out of the box. But it will not be available through browser to add to a list. (I am not sure this will be changed in RTM.)
  • You must install the component "SQLSysClrTypes.msi" on all WFE to get this functionality working.
  • Add the Geolocation field through programming. I prefer to use client object model ECMAScript.
Please follow steps below to test the Geolocation functionality.
  • Create a new page in your site.
  • Add Script Editor web part to the page.
  • From "Edit this web part" option -> click on EDIT SNIPPET button.
  • In the snippet box add below code.
    <script type="text/javascript">
    var context;
    var listName="";
    
    function AddGeoLocationToList() {
    if(document.getElementById("tbList").value.length == 0)
    alert("Please enter the list name");
    context = new SP.ClientContext.get_current();
    this.list = context.get_web().get_lists().getByTitle(document.getElementById("tbList").value);
    context.load(this.list);
    context.executeQueryAsync(
    function () {         
    this.list = context.get_web().get_lists().getByTitle(document.getElementById("tbList").value);
    this.newField = this.list.get_fields().addFieldAsXml(
            "", true, SP.AddFieldOptions.defaultValue
        );
    context.load(this.newField);
    context.executeQueryAsync(     
      Function.createDelegate(this, this.onFieldAddSucceeded),
      Function.createDelegate(this, this.onFieldAddFailed)
    );
       }     
    );
    }
    
    function onFieldAddSucceeded() {
    alert("GeoLocation field is added successfully to the list.");
    }
    
    function onFieldAddFailed(sender, args) {
        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }</script>
    <div>
    List name: <input id="tbList" type="text" value="" />
    <input onclick="AddGeoLocationToList();" type="button" value="Add Geolocation field to the list" />
    </div>
    
  • After you pasted the above code the snippet shows the preview and click OK.
  • Now save your changes and check the page. It should look like below.
  • Enter the list name and click the button to add the Geolocation field to the list given.
Now, when you try to add a new item in the list you will see the options below.

Click Preview Icon to see the map in All items view

Another nice addition to this is, if you add the Geolocation field you will see a new view in the list of views available named "Map View". And when we create a view using this view you will see below UI.

This will be very helpful option for all of us. For mobile, map integration there is no need to write a single extra line of code. The maps you are seeing are Bing/Nokia only. If you like to work with Google maps then you have to do customization by writing some lines of code.

Note: If you have registered with Bing maps and you have the key, please update the key in SharePoint to remove that watermark text over maps. For this you can update at FARM level using powershell.
Set-SPBingMapsKey –BingKey "<Enter a valid Bing Maps key>"
If you like to update at web level then you must use the programming model.

1 comment: