トップ回答者
カスタムリストの列追加&名前変更をVBAかPowerShellでやりたい

質問
-
やりたいことは単純で、SharePointのカスタムリストを新規に作成し、
それにVBAかPowerShellで列を追加し、その後名前を修正したいのです。
SHarePointの列名が、表示名と内部名で異なるため、
このような要望は多いのか、WEBではいくつか参考記事が見つかりましたが、
こちらで検証したところうまく実現できませんでした。
こちらのフォーラムでも過去記事を検索しましたが、
関係した記事が見つかりませんでしたので投稿させていただきます。
よろしくお願いいたします。
===補足情報===
コメントいただけるように、もう少し具体的に、
こちらで書いたスクリプトと、そのエラーを提出いたします。
このリストを実行したときに、
61行目のAddメソッドで次のようなエラーになります。
あちこちのWEBサイトからつぎはぎしていますので、
正しくはどのようなメソッドやtypeを使うべきなのかもわかっておりません。
よろしくお願いいたします。
===エラー表示ここから===
型 [Microsoft.SharePoint.SPFieldType] が見つかりません。
発生場所 C:\Users\fumio\Downloads\AddColumnsToListTable2.ps1:61 文字:26
+ $fields.Add("Honorific", [Microsoft.SharePoint.SPFieldType]::Text, $f ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (Microsoft.SharePoint.SPFieldType:TypeName) []、RuntimeException
+ FullyQualifiedErrorId : TypeNotFound
===エラー表示ここまで======スクリプトここから===
# ##### Windows Power ShellでSharePointListのカラムを追加する
# 接続情報の取り込み
. .\CONNECTION_INFO.ps1
$CONNECT_USER
$CONNECT_PASS
$CONNECT_SITE
$TARGET_LIST_NAME
# SDK の読み込み
Function LoadSP_SDK{
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime") | Out-Null
}
# #####
Function Login_getCred($user, $password){
# ユーザー名
# パスワード
$p = ConvertTo-SecureString $password -AsPlainText -Force;
# SharePoint Online 認証情報
$c = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($user, $p);
return $c;
}
# #####
# ##### ここから開始
# #####
LoadSP_SDK; # SDK の読み込み
$param = @{
user = $CONNECT_USER;
password = $CONNECT_PASS;
}
$credential = Login_getCred @param; # ログインして認証結果取得
#リストが存在するサイトの URL
$siteUrl = $CONNECT_SITE;
# SharePoint Client Context インスタンスを生成
$context = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$context.Credentials = $credential
# ##### ##### #
# ##### 認証通過完了 ##### #
# ##### ##### #
# ##### 名前からリストの指定
$listName = $TARGET_LIST_NAME;
$myCustomList = $context.get_web().get_lists().getByTitle($listName)
# 列の追加
$fields = $myCustomList.Fields
$fields.Add("Honorific", [Microsoft.SharePoint.SPFieldType]::Text, $false)
$context.Dispose()
===スクリプトここまで===
- 編集済み ふみお1965 2019年4月10日 10:29
2019年4月10日 8:28
回答
-
無事にできました。
前掲のscriptの最後の3行、
# 列の追加
以降を次に変更したところ、とりあえず内部名と表示名を区別して列の追加ができました。
# 列の追加
$FieldID = New-Guid
$Name = 'Honorific';
$DisplayName = '敬称';
$Description = "";
$IsRequired = $FALSE;
$EnforceUniqueValues = $FALSE;
$MaxLength = 100;
$FieldSchema = "<Field Type='Text' ID='{$FieldID}' Name='$Name' StaticName='$Name' DisplayName='$DisplayName' Description='$Description' Required='$IsRequired' EnforceUniqueValues='$EnforceUniqueValues' MaxLength='$MaxLength' />"
write-host $FieldSchema
$NewField = $myCustomList.Fields.AddFieldAsXml($FieldSchema,$True,[Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldInternalNameHint)
$context.ExecuteQuery()
$context.Dispose()
- 回答としてマーク ふみお1965 2019年4月11日 1:54
2019年4月11日 1:54
すべての返信
-
英語のサイトも検索したところ、
こういうサイトがありましたので、
これで列の追加はできそうです。
SharePoint Online PowerShell to Add Column to List
If you are looking for SharePoint Online PowerShell to add column to list, such as "Single Line of Text", "Choice", etc. use :
- SharePoint Online: Add Single Line of Text Field to List using PowerShell
- SharePoint Online: Add Multiple Lines of Text Column to List using PowerShell
- SharePoint Online: Add Calculated Column to List using PowerShell
- SharePoint Online: Add Hyperlink or Picture Column to List using PowerShell
- SharePoint Online: Add Lookup Column to List using PowerShell
- SharePoint Online: How to Add Geolocation (Bing Maps) Field to List using PowerShell?
- SharePoint Online: Add Number Column to List using PowerShell
- SharePoint Online: Add Choice Field to List using PowerShell
- SharePoint Online: Add Currency Column to List using PowerShell
- SharePoint Online: Add Managed Metadata Column to List using PowerShell
- SharePoint Online: Add "Yes/No" Check Box Column to List using PowerShell
- SharePoint Online: Add Person or Group (People Picker) Column to List using PowerShell
- SharePoint Online: Create Date and Time Column in List using PowerShell
- SharePoint Online: Add Managed Metadata Column to List using PowerShell
#Read more: http://www.sharepointdiary.com/2017/12/sharepoint-online-add-site-column-to-list-using-powershell.html#ixzz5ki0MFIvc2019年4月10日 16:03 -
無事にできました。
前掲のscriptの最後の3行、
# 列の追加
以降を次に変更したところ、とりあえず内部名と表示名を区別して列の追加ができました。
# 列の追加
$FieldID = New-Guid
$Name = 'Honorific';
$DisplayName = '敬称';
$Description = "";
$IsRequired = $FALSE;
$EnforceUniqueValues = $FALSE;
$MaxLength = 100;
$FieldSchema = "<Field Type='Text' ID='{$FieldID}' Name='$Name' StaticName='$Name' DisplayName='$DisplayName' Description='$Description' Required='$IsRequired' EnforceUniqueValues='$EnforceUniqueValues' MaxLength='$MaxLength' />"
write-host $FieldSchema
$NewField = $myCustomList.Fields.AddFieldAsXml($FieldSchema,$True,[Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldInternalNameHint)
$context.ExecuteQuery()
$context.Dispose()
- 回答としてマーク ふみお1965 2019年4月11日 1:54
2019年4月11日 1:54