Friday, September 07, 2012

How to add a Geolocation field to SharePoint 2013 lists using powershell


There is a great resource on MSDN that shows how to add a geolocation field to a SharePoint 2013 list using the client object model. It may not always be the handiest way to do this however, and you may want to do this using powershell instead. Here is the code for that.
Param(
 [parameter(Mandatory=$true)]
 $webUrl,
 [parameter(Mandatory=$true)]
 $listName
 )
 
Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue

$fieldXml = "<Field Type='Geolocation' DisplayName='Location' />"

$web = Get-SPWeb $webUrl
$list = $web.Lists[$listName]

$list.Fields.AddFieldAsXml($fieldXml,$true,[Microsoft.SharePoint.SPAddFieldOptions]::Default)
$list.Update()
Save this to a file named Add-SPGeoField.ps1 file and you can execute the following command from powershell
  Add-SPGeoField.ps1 -webUrl http://someweb -listName SomeList
This will add a field named Location to list SomeList located in web at http://someweb. Note that the field name is hardcoded in the script but can very easily be added as a parameter if so desired.

2 comments:

ldsouza said...

Hi Joe,

Do you have any idea how to do a bulk update of this geolocation field for many items?

Joe Capka said...

I can think of a script or code that uses a looping construct to do the update, see the MSDN link in the article on how to update one item and then just loop.

A bulk update however, I am not sure.