Add a Geolocation Field to SharePoint Online using PowerShell


I’ve been doing a lot of work with Office 365 & SharePoint Online recently, and one of the things I really wanted to experiment/play with was the new geolocation field. While I could’ve done this with one of my (on-premise) Virtual Machines, I’m working towards getting a better grasp on what is available in the cloud. That said, I quickly found out you can’t create a geolocation field using the UI. In a quick search I found Tobias’ blog, which had just about all of the information I needed to get started. Unfortunately, talking to Office 365 using Windows PowerShell is a bit more involved than an on-prem server is.

After adapting some of my other PowerShell code to Office 365 compatible recently, I had a good understanding of how to talk to the API using what is essentially client-side code. Below is a simple PowerShell example on how to do this. Obviously you’ll want to change variables such as $WebUrl, $EmailAddress, etc. I haven’t made this a nice pretty function yet, but I wanted to get this up to hopefully provide some help to others who may be trying the same things as me.

This example below assumes you already have a list called “ClientLocations” and that you want to use the name “Office Location” for your field. Thanks to Tobias for providing the majority of the work, I just adapted it for O365. 🙂

Cheers!

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime") | Out-Null
$WebUrl = 'https://yourtenant.sharepoint.com/sites/somesite'
$EmailAddress = "username@yourtenant.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("ClientLocations")
$FieldXml = "<Field Type='Geolocation' DisplayName='Office Location'/>"
$Option=[Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldToDefaultView
$List.Fields.AddFieldAsXml($fieldxml,$true,$option)
$Context.Load($list)
$Context.ExecuteQuery()
$Context.Dispose()
Advertisement

One thought on “Add a Geolocation Field to SharePoint Online using PowerShell

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s