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

Requirement: Add Geolocation field to display Bing map in a 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 columns from SharePoint UI, but you’ll have to write code to add! Here are the steps to enable the Bing Maps column to the SharePoint Online list:

Step 1: Obtain Bing Maps Key

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

  1. 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.
  2. 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
}

The same thing can be done with PnP PowerShell as:

#Parameters
$SiteURL = "https://Crescent.sharepoint.com/sites/Marketing"
$BingMapKey = "Your Bing Map Key"

#Connect to Site
Connect-PnPOnline -url $SiteURL -Interactive

#Set Bing Map Key
Set-PnPPropertyBagValue -Key "BING_MAPS_KEY" -Value $BingMapKey

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 a geolocation column to the 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

PnP PowerShell to Add a Geolocation Field to List

#Parameter
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$ListName = "Metrics"
$FieldName = "Location"
 
Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Interactive
  
    #Get the List
    $List = Get-PnPList -Identity $ListName
    
    #Add Geolocation field to the List
    Add-PnPField -List $List -Type GeoLocation -DisplayName $FieldName -InternalName $FieldName -AddToDefaultView -AddToAllContentTypes

    Write-host "Bing Map Geolocation Field Added to the List!" -f Green
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

And here is the result of the 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 - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

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

  • 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

Your email address will not be published. Required fields are marked *