SharePoint Online: Como añadir un campo de tipo geolocalización a una lista con PowerShell!

En esta ocasión os comparto un nuevo script en el que se muestra como añadir un campo de tipo geolocalización a una lista de un sitio de SharePoint Online por medio de PowerShell. Como podéis ver en el script (How to add a gelocalization field to a list in a SharePoint Online Site), añadir un campo de tipo gelocalización a la lista es realmente sencillo puesto que sólo necesitamos aplicar las reglas de uso del modelo de objetos en cliente (CSOM) de SharePoint para añadir a la lista (objeto List) un campo de este tipo haciendo uso del método AddFieldAsXml() disponible en la colección Fields.

   1: ############################################################################################################################################

   2: #Script that adds a geolocalization field to a list in a SharePoint Online site

   3: # Required Parameters:

   4: #  -> $sUserName: User Name to connect to the SharePoint Online Site Collection.

   5: #  -> $sPassword: Password for the user.

   6: #  -> $sSiteUrl: SharePoint Online Site Url

   7: #  -> $sListName: Name of the list where the geolocalization field is going to be added.

   8: ############################################################################################################################################

   9:  

  10: $host.Runspace.ThreadOptions = "ReuseThread"

  11:  

  12: #Definition of the function that allows to add a geolocalization field to a list in SharePoint Online

  13: function Add-GeolocalizationFieldSPO

  14: {

  15:     param ($sSiteUrl,$sUserName,$sPassword,$sListName)

  16:     try

  17:     {    

  18:         #Adding the Client OM Assemblies        

  19:         Add-Type -Path "<CSOM_Path>\Microsoft.SharePoint.Client.dll"

  20:         Add-Type -Path "<CSOM_Path>\Microsoft.SharePoint.Client.Runtime.dll"

  21:  

  22:         #SPO Client Object Model Context

  23:         $spoCtx = New-Object Microsoft.SharePoint.Client.ClientContext($sSiteUrl)

  24:         $spoCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($sUserName, $sPassword)  

  25:         $spoCtx.Credentials = $spoCredentials      

  26:  

  27:         Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green

  28:         Write-Host "Adding a gelocalization field to $sListName !!" -ForegroundColor Green

  29:         Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green

  30:         

  31:         $spoSite=$spoCtx.Site

  32:         $spoList=$spoCtx.Web.Lists.GetByTitle($sListName)        

  33:         $FieldOptions=[Microsoft.SharePoint.Client.AddFieldOptions]::AddToAllContentTypes

  34:         $spoList.Fields.AddFieldAsXml("<Field Type='Geolocation' DisplayName='Location'/>",$true,$FieldOptions)

  35:         $spoList.Update()

  36:         $spoCtx.ExecuteQuery()

  37:         $spoCtx.Dispose()

  38:     }

  39:     catch [System.Exception]

  40:     {

  41:         write-host -f red $_.Exception.ToString()   

  42:     }    

  43: }

  44:  

  45: #Required Parameters

  46: $sSiteUrl = "https://<SPO_Site_Url>" 

  47: $sUserName = "<SPOUser>@<SPODomain>.onmicrosoft.com" 

  48: $sListName= "<ListName>"

  49: #$sPassword = Read-Host -Prompt "Enter your password: " -AsSecureString  

  50: $sPassword=convertto-securestring "<SPOPassword>" -asplaintext -force

  51:  

  52: Add-GeolocalizationFieldSPO -sSiteUrl $sSiteUrl -sUserName $sUserName -sPassword $sPassword -sListName $sListName

  53:  

Si ejecutáis el script anterior para un sitio de SharePoint Online y una lista denominada Locations:

  • Veréis como la columna es añadida a la lista.
  • Podréis añadir elementos a la lista y para el campo Location podréis especificar o bien las coordenadas o bien la ubicación actual. Como podéis ver, al añadir la ubicación se muestra el correspondiente mapa de Bing Maps junto con el siguiente mensaje: “The specified credentials are invalid. You can sign uo fir a free devekioer account at http://www.bingmapsportal,com
image image
  • Lo mismo sucede en la preview del mapa una vez se ha añadido el elemento a la lista.

image

Para que desaparezca dicho mensaje, tendréis que especificar en la colección de sitios de SharePoint Online la correspondiente clave de Bing Maps haciendo uso del siguiente script (How to add a Bing Maps Key to a SharePoint Online Site Collection) en el que de nuevo se hace uso del CSOM para en este caso acceder a la property bag BING_MAPS_KEY y actualizarla con la clave de Bing Maps correspondiente:

   1: ############################################################################################################################################

   2: #Script that adds a Bing Maps Key to a SharePoint Online Site Collection

   3: # Required Parameters:

   4: #  -> $sUserName: User Name to connect to the SharePoint Online Site Collection.

   5: #  -> $sPassword: Password for the user.

   6: #  -> $sSiteUrl: SharePoint Online Site Url.

   7: #  -> $sBingMapsKey: Bing Maps Key to be added.

   8: ############################################################################################################################################

   9:  

  10: $host.Runspace.ThreadOptions = "ReuseThread"

  11:  

  12: #Definition of the function that allows to add a Bing Maps Key to a SharePoint Onlione Site Collection

  13: function Add-BingMapsKey

  14: {

  15:     param ($sSiteUrl,$sUserName,$sPassword,$sBingMapsKey)

  16:     try

  17:     {    

  18:         #Adding the Client OM Assemblies        

  19:         Add-Type -Path "H:3 Docs\10 MVP3 MVP Work\11 PS Scripts\Office 365\Microsoft.SharePoint.Client.dll"

  20:         Add-Type -Path "H:3 Docs\10 MVP3 MVP Work\11 PS Scripts\Office 365\Microsoft.SharePoint.Client.Runtime.dll"

  21:  

  22:         #SPO Client Object Model Context

  23:         $spoCtx = New-Object Microsoft.SharePoint.Client.ClientContext($sSiteUrl)

  24:         $spoCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($sUserName, $sPassword)  

  25:         $spoCtx.Credentials = $spoCredentials      

  26:  

  27:         Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green

  28:         Write-Host "Adding Bing Maps Key to $sSiteUrl !!" -ForegroundColor Green

  29:         Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green

  30:         

  31:         $spoSiteCollection=$spoCtx.Site

  32:         $spoCtx.Load($spoSiteCollection)

  33:         $spoRootWeb=$spoSiteCollection.RootWeb

  34:         $spoCtx.Load($spoRootWeb)        

  35:         $spoAllSiteProperties=$spoRootWeb.AllProperties

  36:         $spoCtx.Load($spoAllSiteProperties)

  37:         $spoRootWeb.AllProperties["BING_MAPS_KEY"]=$sBingMapsKey

  38:         $spoRootWeb.Update()        

  39:         $spoCtx.ExecuteQuery()

  40:         $spoCtx.Dispose()

  41:     }

  42:     catch [System.Exception]

  43:     {

  44:         write-host -f red $_.Exception.ToString()   

  45:     }    

  46: }

  47:  

  48: #Required Parameters

  49: $sSiteUrl = "https://<SPO_Site_Url>" 

  50: $sUserName = "<SPOUser>@<SPODomain>.onmicrosoft.com" 

  51: $sBingMapsKey= "<BingMaps_Key>"

  52: #$sPassword = Read-Host -Prompt "Enter your password: " -AsSecureString  

  53: $sPassword=convertto-securestring "<SPO_Password>" -asplaintext -force

  54:  

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

  56:  

Si ejecutáis el script anterior y de nuevo hacéis clic en la previsualización del mapa, veréis como el mensaje ha desaparecido.

image

Referencias:

Un pensamiento en “SharePoint Online: Como añadir un campo de tipo geolocalización a una lista con PowerShell!

  1. Pingback: SharePoint 2013: Resumen de posts (LXI)! | Pasión por la tecnología...

Deja una respuesta

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Salir /  Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Salir /  Cambiar )

Conectando a %s