SharePoint Online: How to add a gelocalization field to a list using PowerShell!

This time I’m sharing a new PowerShell script where I demonstrate how to add a gelocalization field to a list in a SharePoint Online site. As you can see in the script (How to add a gelocalization field to a list in a SharePoint Online Site), adding a geolocalization field to an existing list is really easy since you only need to follow the Client Side Object Modelo (CSOM) rules in SharePoint Online. In this case, in order to add the new field to the list (List object) I’m using the AddFieldAsXml() method defined in the Fields collection.

   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:  

If you run this script against a SharePoint Online site and for a list named Locations:

  • You will see that the new column is added to the list and it’s available in the default view.
  • You will be able to add new list elements and for the “Location” field you will be able to specify a custom location or the current one. As you can see, once you have added the location a Bing Map is shown and also the following message: “The specified credentials are invalid. You can sign uo fir a free devekioer account at http://www.bingmapsportal,com”.
image_thumb[1] image_thumb[2]
  • Of course, the same situation happens in the map preview once the list element is added to the list.

image_thumb[3][4]

In order to remove such message, you will have to specify for the specific SharePoint Online site collection the related Bing Maps key using the following PowerShell Script (How to add a Bing Maps Key to a SharePoint Online Site Collection) that uses the CSOM to access the BING_MAPS_KEY property bag and update it with a Bing Maps key:

   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:  

If you run the script and you preview the map again, you will see that the Bing Maps message is not shown any more.

image_thumb[4]

References:

2 pensamientos en “SharePoint Online: How to add a gelocalization field to a list using PowerShell!

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

  2. Pingback: SharePoint 2013: Resumen de posts (LXI)! - Blog de Juan Carlos González en Geeks.MS

Responder

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. Cerrar sesión /  Cambiar )

Google photo

Estás comentando usando tu cuenta de Google. Cerrar sesión /  Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión /  Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión /  Cambiar )

Conectando a %s