SharePoint Online: How to Add Geolocation (Bing Maps) Field to List using PowerShell?

Requirement: Add Geolocation field to display Bing map in list using PowerShell in SharePoint Online.

How to Add Geo-Location Column to SharePoint Online List?

The geolocation field lets you enter location information as a pair of latitude and longitude coordinates and display it in a Bing Map. So, How to add a Geolocation column in SharePoint Online, because you don’t find this column from SharePoint UI?

Well, the Geolocation column is not available in SharePoint lists columns or site column from SharePoint UI, but you’ll have to write code to add! Here are the steps to enable the Bing Maps column to SharePoint online list.

Step 1: Obtain Bing Maps Key

To enable geolocation field in SharePoint Online list, we need to obtain Bing maps keys first.

  • Go to Bing Maps dev center https://www.bingmapsportal.com/ and sign-in (or Sign-up) with your Microsoft account. Create a new Bing map account.
  • Go to:  My Account >> My keys. Click on “create a new key” >> Fill in the Application name and Key type >> Click on “Create”. You’ll get the new key in “list of available keys” momentarily!
    sharepoint online geolocation field

Step 2: Set Bing Maps Key to SharePoint Online:

Once the Bing maps key is obtained by the above steps, you’ll have to update it to your SharePoint online site. Use this PowerShell to set Bing maps key to SharePoint Online.

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
   
#Variables for Processing
$SiteURL="https://crescent.sharepoint.com"
$BingKey="AkDeSbNMX7oTm3jqv69D3Eq1hlN72sdUQ3PdnwbUWQmVo4a7QyH97P3IN2fA5AZb"

Try {
    #Get Credentials to connect
    $Cred = Get-Credential
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
 
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Credentials
 
    $Web = $Ctx.Web
    $Web.AllProperties['BING_MAPS_KEY'] = $BingKey
    $Web.Update()
    $Ctx.ExecuteQuery()
 
    Write-host "Bing Map Key Updated Successfully!" -ForegroundColor Green
 }
Catch {
    write-host -f Red "Error Updating Bing Map Key!" $_.Exception.Message
}

Step 3: Add Bing Map Geo Location Column to SharePoint Online List:

As this column type isn’t available from SharePoint UI, Let’s use PowerShell to add geolocation column to list.

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Custom function to add column to list
Function Add-GeolocationColumnToList()
{ 
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ListName,
        [Parameter(Mandatory=$true)] [string] $Name,
        [Parameter(Mandatory=$true)] [string] $DisplayName,
        [Parameter(Mandatory=$false)] [string] $Description=[string]::Empty
    )

    #Generate new GUID for Field ID
    $FieldID = New-Guid

    Try {
        $Cred= Get-Credential
        $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Credentials
        
        #Get the List
        $List = $Ctx.Web.Lists.GetByTitle($ListName)
        $Ctx.Load($List)
        $Ctx.ExecuteQuery()

        #Check if the column exists in list already
        $Fields = $List.Fields
        $Ctx.Load($Fields)
        $Ctx.executeQuery()
        $NewField = $Fields | where { ($_.Internalname -eq $Name) -or ($_.Title -eq $DisplayName) }
        if($NewField -ne $NULL)  
        {
            Write-host "Column $Name already exists in the List!" -f Yellow
        }
        else
        {
            #Create Column in the list
            $FieldSchema = "<Field Type='Geolocation' ID='{$FieldID}' DisplayName='$DisplayName' Name='$Name' Description='$Description'></Field>"
            $NewField = $List.Fields.AddFieldAsXml($FieldSchema,$True,[Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldInternalNameHint)
            $Ctx.ExecuteQuery()    
            
            Write-host "New Column Added to the List Successfully!" -ForegroundColor Green  
        }
    }
    Catch {
        write-host -f Red "Error Adding Column to List!" $_.Exception.Message
    }
} 

#Set parameter values
$SiteURL="https://crescent.sharepoint.com"
$ListName="Projects"
$Name="ProjectLocation"
$DisplayName="Project Location"
$Description="Location of the Project"

#Call the function to add column to list
Add-GeolocationColumnToList -SiteURL $SiteURL -ListName $ListName -Name $Name -DisplayName $DisplayName -Description $Description

and here is the result of SharePoint Online Geolocation field:

bing map geolocation field to sharepoint online list

Using Bing Maps with SharePoint On-Premises was explained in my another article: How to Add Bing Maps Geolocation column in SharePoint

Tips: You can create a “map view” for better visualization. Map view displays a map (with data obtained from the Bing Maps service), using longitude and latitude entries from the Geolocation field type.

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

One thought on “SharePoint Online: How to Add Geolocation (Bing Maps) Field to List using PowerShell?

  • December 5, 2018 at 2:02 AM

    Do you know if there is a way to change the default view of the map from Street View to Aerial / Satellite view? I’ve got this all working in a SharePoint Online List, but I cant figure out how to change the map view.

    Reply

Leave a Reply