💻

PowerShell と CSOM で Geolocation 列に値を入れるときにちょっとハマったのでメモ

2022/01/01に公開

相変わらず PowerShell なんですけれども。

Geolocation 列に値を入れる方法については以下にまとまっています。FieldGeolocationValue を使ってねということのようです。

https://docs.microsoft.com/en-us/sharepoint/dev/general-development/how-to-add-a-geolocation-column-to-a-list-programmatically-in-sharepoint?WT.mc_id=M365-MVP-5002941

それでは実際にやってみます。

$item = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation
$item = $list.AddItem($item)

$value = New-Object Microsoft.SharePoint.Client.FieldGeolocationValue
$value.Latitude = 10
$value.Longitude = 10
$item["Location"] = $value

$item.Update()
$ctx.Load($item)
$ctx.ExecuteQuery()

実行すると?

"0" 個の引数を指定して "ExecuteQuery" を呼び出し中に例外が発生しました: "The geolocation value does not represent a geo graphical point. Either the value is not in the correct format or it is not in the valid range."
発生場所 行:1 文字:1
+ $ctx.ExecuteQuery()
+ ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ServerException

正解はこっち。値を明示的に指定してあげる必要があります。

$item = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation
$item = $list.AddItem($item)

$value = New-Object Microsoft.SharePoint.Client.FieldGeolocationValue
$value.Latitude = 10
$value.Longitude = 10
$item["Location"] = [Microsoft.SharePoint.Client.FieldGeolocationValue]$value

$item.Update()
$ctx.Load($item)
$ctx.ExecuteQuery()

ちなみに FieldLookupValueFieldUserValue でも同じ現象が発生します。もっともこちらは ID を指定して更新できるので使わないという手もありますけど。

Discussion