2017-04-13 78 views
0

我希望能夠使用PowerShell創建列表和選擇列,但選擇列總是失敗。我發現了幾個例子顯示我的代碼工作,所以我必須錯過一些東西。使用PowerShell創建SharePoint選項字段

MSDN Link to Add field

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 
+0

添加fieldAsXml而不是寫這麼多的代碼。 – Vaibhav

+0

「我的一堆代碼不起作用:* splat *」不是一個好問題。失敗如何?它應該做什麼,它做了什麼?它給了什麼錯誤?你曾經嘗試過什麼來隔離代碼行,或者修復它? – TessellatingHeckler

+0

我不好,我忘了添加錯誤的詳細信息。 – Swazimodo

回答

1

這是一個不理解PowerShell如何處理參數和返回值的情況。

當調用函數並傳入參數時,如果不使用參數名稱,它會假定您將數組傳遞給函數的第一個參數。這就是爲什麼Thriggle使用AddFieldAsXml的建議不存在並且失敗了。

對於返回值,函數的所有輸出都作爲數組傳回。我使用$ return [-1]從返回值中獲取最後一個參數,並將其保存爲我想要的列表對象。

$return = CreateCustomList $siteCollectionUrl $listName $listTitle $listDescription 
$spList = $return[-1] 
AddChoiceField -spList $spList -title "SR Test List" 
1

您的代碼看起來是正確的,所以必須有別的事情錯了。

在PowerShell中,如果您願意使用XML字符串,AddFieldAsXml()會更容易一些。

由於它只有一個參數(它是一個字符串),PowerShell不太可能與各種對象/參數類型以及潛在或虛構的方法重載混淆。

function AddChoiceField($spList, $title){ 
    $name = $title -replace '\s','' 
    $xml = '<Field Required="FALSE" Description="" DisplayName="' + $name + 
     '" Type="Choice" Format="Dropdown" FillInChoice="FALSE">' + 
     '<CHOICES><CHOICE>Completed</CHOICE><CHOICE>Pending</CHOICE><CHOICE>Not Applicable</CHOICE></CHOICES>' + 
     '</Field>' 
    $spList.Fields.AddFieldAsXml($xml); 
    $spList.Update(); 
    $spCol = $spList.Fields[$name] 
    $spCol.Title = $title 
    $spCol.Update() 
} 
+0

這可能是一種解決方法,但是我的代碼出了什麼問題。我發現了其他一些做同樣事情的例子。 http://www.dotnetsharepoint.com/2013/06/how-to-create-list-using-power-shell.html#.Upem3cROMnU – Swazimodo

+0

我剛測試過這個,並失敗「方法調用失敗,因爲[Microsoft.SharePoint。 SPField]不包含名爲'AddFieldAsXml'的方法「 – Swazimodo

+0

@SamNesbitt這很好奇。 '$ spList.Fields'應該是SPFieldCollection類型,而不是SPField。也許你需要對'$ spList'參數值做一點調查,並確保它是你期望的。 – Thriggle