Create Geolocation Field and Map View

Create Geolocation Field and Map View

There is a column type called Geolocation. This column type can only be added to a library or list programatically. It is useful for users who need to track locations and see it all in an aggregated map view.

  1. Copy this Power Shell Script, update the parameters for username, site URL and the list/library name. (items in bold)Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue[System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint.Client”) | Out-Null
    [System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint.Client.Runtime”) | Out-Null
    $WebUrl = ‘https://<SPO_Site_Url>
    $EmailAddress = “<SPOUser>@<SPODomain>.onmicrosoft.com
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($WebUrl)
    $Credentials = Get-Credential -UserName $EmailAddress -Message “Please enter your Office 365 Password”
    $Context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($EmailAddress,$Credentials.Password)
    $List = $Context.Web.Lists.GetByTitle(“List Name“)
    $FieldXml = “<Field Type=’Geolocation’ DisplayName=’Geolocation’/>”
    $Option=[Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldToDefaultView
    $List.Fields.AddFieldAsXml($fieldxml,$true,$option)
    $Context.Load($list)
    $Context.ExecuteQuery()
    $Context.Dispose()
  2. Run the Power Shell Script
  3. Create a Bing account if you don’t have one already and get a Bing map key here: Bing Maps Account Center
  4. Next run the script here for setting the Bing Maps Key. Update the parameters for Site URL, Username and Bings Map Key.$host.Runspace.ThreadOptions = “ReuseThread”#Definition of the function that allows to add a Bing Maps Key to a SharePoint Onlione Site Collection
    function Add-BingMapsKey
    {
    param ($sSiteUrl,$sUserName,$sPassword,$sBingMapsKey)
    try
    {
    #SPO Client Object Model Context
    $spoCtx = New-Object Microsoft.SharePoint.Client.ClientContext($sSiteUrl)
    $spoCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($sUserName, $sPassword)
    $spoCtx.Credentials = $spoCredentials

    Write-Host “—————————————————————————-” -foregroundcolor Green
    Write-Host “Adding Bing Maps Key to $sSiteUrl !!” -ForegroundColor Green
    Write-Host “—————————————————————————-” -foregroundcolor Green

    $spoSiteCollection=$spoCtx.Site
    $spoCtx.Load($spoSiteCollection)
    $spoRootWeb=$spoSiteCollection.RootWeb
    $spoCtx.Load($spoRootWeb)
    $spoAllSiteProperties=$spoRootWeb.AllProperties
    $spoCtx.Load($spoAllSiteProperties)
    $spoRootWeb.AllProperties[“BING_MAPS_KEY”]=$sBingMapsKey
    $spoRootWeb.Update()
    $spoCtx.ExecuteQuery()
    $spoCtx.Dispose()
    }
    catch [System.Exception]
    {
    write-host -f red $_.Exception.ToString()
    }
    }

    #Required Parameters
    $sSiteUrl = “https://<SPO_Site_Url>
    $sUserName = “<SPOUser>@<SPODomain>.onmicrosoft.com
    $sBingMapsKey= “<BingMaps_Key>
    $sPassword = Read-Host -Prompt “Enter your password: ” -AsSecureString
    #$sPassword=convertto-securestring “<SPO_Password>” -asplaintext -force

    Add-BingMapsKey -sSiteUrl $sSiteUrl -sUserName $sUserName -sPassword $sPassword -sBingMapsKey $sBingMapsKey

  5. The column will be added to the list or library. When you edit an item you can enter in coordinates or use your current location. When you select specify location, a pop up displays to specifiy the coordinates. Once you select ok, a map will display in the list item.

    The Geolocation displays like this in the all items view. When you click on the world icon, the map image pops up.

    In Modern SharePoint, the geolocation field displays like this. No map image displays.
  6. Once the column is added and Bing Maps is configured, when you create a view the option to create a Map view will appear.
  7. In the map view, the user can filter and sort the items displaying based on the metadata columns displaying. Filter to only show points meeting a specific criteria etc. In the screenshot below, I added a year and month column to be able to sort/filter the content.
  8. You can set the geolocation field via a SharePoint Designer workflow or programmatically.
  9. In one business scenario, the users submit the images via PowerApp to a SharePoint library. The coordinates were pulled from the image properties using an Azure Web API and populated in single line of text fields, one for latitude the other for longitude. The coordinates pulled from the image properties was then used to set the geolocation field using a 2010 workflow.
  10. Here is how to create the workflow:
    • Create a 2010 SharePoint List workflow – this only works in a 2010 version workflow, 2013 throws an error.
    • Create a variable and set the value to POINT ([Longitude] [Latitude]). The latitidue and longitude values should be pulled from the column they are being stored in.
    • Then set the Geolocation field to this variable