Swazimodo
Swazimodo

Reputation: 1249

Create SharePoint Choice Fields with PowerShell

I want to be able to create a list and choice column with PowerShell but the Choice column always fails. I have found a few examples showing my code to work so I must be missing something.

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"

Edit: Here is the error message returned

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

Upvotes: 1

Views: 3590

Answers (2)

Swazimodo
Swazimodo

Reputation: 1249

This was a case of not understanding how PowerShell handled parameters and return values.

When calling functions and passing in parameters, if you do not use the parameter name it assumes your passing in an array to the first parameter of the function. This is why Thriggle's suggestion of using AddFieldAsXml did not exist and failed for me.

For return values, all outputs from the function are passed back as an array. I used $return[-1] to grab the last parameter from the return value and save that as the list object that I wanted.

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

Upvotes: 1

Thriggle
Thriggle

Reputation: 7059

Your code looks correct, so there must be something else going wrong.

In PowerShell, AddFieldAsXml() does tend to be a little easier if you're willing to play with strings of XML.

Since it only has one parameter (and it's a string) PowerShell is less likely to get confused with various object/parameter types and potential or imaginary method overloads.

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

Upvotes: 1

Related Questions