Export (0) Print
Expand All

How to: Add a Geolocation column to a list programmatically in SharePoint 2013

SharePoint 2013

Learn how to programmatically add a Geolocation column to a list in SharePoint 2013. Integrate location information and maps in SharePoint lists and location-based websites by using the new Geolocation field creating your own Geolocation-based field type.

Last modified: November 08, 2013

Applies to: SharePoint Foundation 2013 | SharePoint Server 2013

SharePoint 2013 introduces a new field type named Geolocation that enables you to annotate SharePoint lists with location information. In columns of type Geolocation, you can enter location information as a pair of latitude and longitude coordinates in decimal degrees or retrieve the coordinates of the user’s current location from the browser if it implements the W3C Geolocation API. For more information about the Geolocation column, see Integrating location and map functionality in SharePoint 2013. The Geolocation column is not available by default in SharePoint lists. To add the column to a SharePoint list, you have to write code. In this article, learn how to add the Geolocation field to a list programmatically by using the SharePoint client object model.

An MSI package named SQLSysClrTypes.msi must be installed on every SharePoint front-end web server to view the geolocation field value or data in a list. This package installs components that implement the new geometry, geography, and hierarchy ID types in SQL Server 2008. By default, this file is installed for SharePoint Online. However, it is not for an on-premises deployment of SharePoint Server 2013. You must be a member of the Farm Administrators group to perform this operation. To download SQLSysClrTypes.msi, see Microsoft SQL Server 2008 R2 SP1 Feature Pack for SQL Server 2008, or Microsoft SQL Server 2012 Feature Packfor SQL Server 2012 in the Microsoft Download Center.

  • Access to a SharePoint 2013 list, with sufficient privileges to add a column.

  • A valid Bing Maps key set at the farm or web level, which can be obtained from the Bing Maps Account Center.

    Important note Important

    Please note that you are responsible for compliance with terms and conditions applicable to your use of the Bing Maps key, and any necessary disclosures to users of your application regarding data passed to the Bing Maps service.

  • Visual Studio 2010.

Follow these steps to add the Geolocation column to a list using the SharePoint 2013 client object model.

To add the Geolocation column to a list using the client object model

  1. Start Visual Studio.

  2. On the menu bar, choose File, New Project. The New Project dialog box opens.

  3. In the New Project dialog box, choose C# in the Installed Templates box, and then choose the Console Application template.

  4. Give the project a name, and then choose the OK button.

  5. Visual Studio creates the project. Add a reference to the following assemblies, and choose OK.

    Microsoft.SharePoint.Client.dll

    Microsoft.SharePoint.Client.Runtime.dll

  6. In the default .cs file, add a using directive as follows.

    using Microsoft.SharePoint.Client;

  7. Add the following code to the Main method in the .cs file.

    class Program
        {
            static void Main(string[] args)
            {
                AddGeolocationField();
                Console.WriteLine("Location field added successfully");
            }
            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='Location'/>",true, AddFieldOptions.AddToAllContentTypes);                                        
                oList.Update();
                context.ExecuteQuery();
            } 
        }
    
  8. Replace <Site Url> and <List Title> with valid values.

  9. Set the target framework in Project Properties as .NET Framework 4.0 or 3.5, and run the example.

  10. Navigate to the list. You should be able to see a column named Location of type Geolocation in the list. You can now enter some values and see it in action. Figure 1 shows the default location and map features that you can expect to see in your list.

    Figure 1. Summarized view of the default location and map features

    Default Geolocation and Map feature

After the Geolocation field is added to a SharePoint list, the developer can add the list item to the list programmatically. There are two ways to add the list item programmatically: by passing the FieldGeolocationValue object to the Geolocation field, and by passing Raw Value to the Geolocation field.

Method A: Pass the FieldGeolocationValue object to the Geolocation field

  • The following method adds a list item by passing the Geolocation value as an object.

    private void AddListItem()
            {   // Replace site URL and List Title with Valid values.
                ClientContext context = new ClientContext("<Site Url>");
                List oList = context.Web.Lists.GetByTitle("<List Name>");
    
                ListItemCreationInformation itemCreationInfo = new ListItemCreationInformation();
                ListItem oListItem = oList.AddItem(itemCreationInfo);
    
                oListItem["Title"] = "New Title";
    
                FieldGeolocationValue oGeolocationValue = new FieldGeolocationValue();
                oGeolocationValue.Latitude = (double)17.4;
                oGeolocationValue.Longitude = (double)78.4;
                oListItem["location"] = oGeolocationValue;
    
                oListItem.Update();
                context.ExecuteQuery();
            }
    
    

Method B: Pass a raw value to the Geolocation field

  • The following method adds a list item to the SharePoint list by passing raw values to the Geolocation field.

          private void AddListItem()
            {   // Replace site URL and List Title with Valid values.
                ClientContext context = new ClientContext("<Site Url>");
                List oList = context.Web.Lists.GetByTitle("<List Name>");
    
                ListItemCreationInformation itemCreationInfo = new ListItemCreationInformation();
                ListItem oListItem = oList.AddItem(itemCreationInfo);
    
                oListItem["Title"] = "New Title";
                 // Data in WKT (World Known Text) format.
                oListItem["location"] = "POINT (78.4 17.4)" ; 
    
                oListItem.Update();
                context.ExecuteQuery();
            }
    
    
Show:
© 2014 Microsoft