kwizcom banner advertisement

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?
Geolocation filed lets you to enter location information as a pair of latitude and longitude coordinates and display it in a Bing Map. So, How to add Geolocation column in SharePoint online, because you don’t find this column from SharePoint UI?

Well, 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 Bing Maps column to SharePoint online list.

Step 1: Obtain Bing Maps Key
  • 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 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, Lets 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=""
    )

    #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.
SharePoint Online: How to Add Geolocation (Bing Maps) Field to List using PowerShell? SharePoint Online: How to Add Geolocation (Bing Maps) Field to List using PowerShell? Reviewed by Salaudeen Rajack on 12:21 PM Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.