Búsqueda de sitios web

SharePoint Online: ¿Cómo agregar un campo de geolocalización (Bing Maps) a la lista usando PowerShell?


Requisito: Agregue un campo de Geoubicación para mostrar el mapa de Bing en una lista usando PowerShell en SharePoint Online.

¿Cómo agregar una columna de ubicación geográfica a la lista de SharePoint Online?

El campo de geolocalización le permite ingresar información de ubicación como un par de coordenadas de latitud y longitud y mostrarla en un mapa de Bing. Entonces, ¿cómo se agrega una columna de Geolocalización en SharePoint Online porque no se encuentra esta columna en la interfaz de usuario de SharePoint?

Bueno, la columna Geolocalización no está disponible en las listas de SharePoint ni en las columnas del sitio desde la interfaz de usuario de SharePoint, ¡pero tendrás que escribir código para agregarla! Estos son los pasos para habilitar la columna Bing Maps en la lista de SharePoint Online:

Paso 1: Obtenga la clave de Bing Maps

Para habilitar el campo de geolocalización en la lista de SharePoint Online, primero debemos obtener las claves de mapas de Bing.

  1. Vaya al centro de desarrollo de Bing Maps https://www.bingmapsportal.com/ e inicie sesión (o regístrese) con su cuenta de Microsoft. Cree una nueva cuenta de mapa de Bing.
  2. Vaya a: Mi cuenta >> Mis claves. Haga clic en "crear una nueva clave" >> Complete el nombre de la aplicación y el tipo de clave >> Haga clic en "Crear". ¡Obtendrás la nueva clave en la "lista de claves disponibles" momentáneamente!

Paso 2: configurar la clave de Bing Maps en SharePoint Online

Una vez que los pasos anteriores obtengan la clave de mapas de Bing, deberá actualizarla en su sitio de SharePoint Online. Utilice este PowerShell para configurar la clave del mapa de Bing en 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
}

Se puede hacer lo mismo con PnP PowerShell:


#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

Paso 3: Agregar la columna de ubicación geográfica del mapa de Bing a la lista de SharePoint Online

Como este tipo de columna no está disponible en la interfaz de usuario de SharePoint, usemos PowerShell para agregar una columna de geolocalización a la lista.


#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 para agregar un campo de geolocalización a la lista


#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
}

Y aquí está el resultado del campo Geolocalización de SharePoint Online:

El uso de Bing Maps con SharePoint local se explicó en mi otro artículo: ¿Cómo agregar la columna de geolocalización de Bing Maps en SharePoint?

Consejos: Puede crear una "vista de mapa" para una mejor visualización. La vista de mapa muestra un mapa (con datos obtenidos del servicio Bing Maps), utilizando entradas de longitud y latitud del tipo de campo Geolocalización.

Artículos relacionados