📍

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

に公開

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

PowerShell を使って実際に試してみます。

$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()

なお、FieldLookupValue や FieldUserValue でも同様の現象が発生します。ただし、これらは ID を指定して更新できるため、利用しないという選択肢もあります。

Discussion