PowerShellでリストと選択列を作成できますが、選択列は常に失敗します。私は何かが欠けているはずなので、私のコードが動作することを示すいくつかの例を見つけました。PowerShellを使用してSharePointの選択フィールドを作成する
clear
Add-PSSnapin Microsoft.SharePoint.PowerShell
function CreateCustomList($siteCollectionUrl, $listName, $listTitle, $listDescription) {
$spWeb = Get-SPWeb -Identity $siteCollectionUrl
$spTemplate = $spWeb.ListTemplates["Custom List"]
$spListCollection = $spWeb.Lists
$spListCollection.Add($listName, $listDescription, $spTemplate)
$path = $spWeb.url.trim()
#set display name
$spList = $spWeb.GetList("$path/Lists/$listName")
$spList.Title = $listTitle
$spList.Update()
return $spList
}
function AddChoiceField($spList, $title){
#create choices collection
$choices = New-Object System.Collections.Specialized.StringCollection
$choices.Add("Completed") | Out-Null
$choices.Add("Pending") | Out-Null
$choices.Add("Not Applicable") | Out-Null
#create choice field
$name = $title -replace '\s',''
$spFieldType = [Microsoft.SharePoint.SPFieldType]::Choice
$name = $spList.Fields.Add($name,
[Microsoft.SharePoint.SPFieldType]::Choice,
$false,
$false,
$choices);
#Set display Name
$spCol = $spList.Fields[$name]
$spCol.Title = $title
$spCol.Update()
}
$siteCollectionUrl = "http://URL"
$listName = "CustomCheckList"
$listTitle = "Custom Checklist"
$listDescription = "This checklist is used because of reasons"
$spList = CreateCustomList $siteCollectionUrl $listName $listTitle $listDescription
AddChoiceField $spList, "First checklist item"
編集: ここでエラーメッセージがあなたのコードが正しいようなので、間違って何か他のものが存在しなければならない
Cannot find an overload for "Add" and the argument count: "5".
At line:11 char:5
+ $name = $spList.Fields.Add($name,
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodCountCouldNotFindBest
Exception setting "Title": "Object reference not set to an instance of an object."
At line:19 char:5
+ $spCol.Title = $title
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], SetValueInvocationException
+ FullyQualifiedErrorId : ExceptionWhenSetting
この多量のコードをwritttingではなく、fieldAsXmlを追加します。 – Vaibhav
"私の山ほどのコードは機能しません:*スプラット*"は良い質問ではありません。どのように失敗する?それは何をしなければならないのですか?それはどのようなエラーですか?問題のコード行を分離する、または修正するために何をやってみましたか? – TessellatingHeckler
私の悪い私は、エラーの詳細を追加することを忘れていた。 – Swazimodo