1

We have 150 customer portals in our SP site collection, all sites are a templated copy of each other - i.e. they all have an Internal Documents, Working Documents and Final Documents.

Inside each document library, there is a choice field called "Assessment Categories". I have been asked to add another choice called Declaration.

So I basically have 450 "Assessment Categories" Fields that need a new choice added: Declaration.

I have created a .csv file with two columns:

Name         URL
Client 1     http://sp-client1
Client 2     http://sp-client2
Client 3     http://sp-client3

And this is the code I have created so far:

# PowerShell script to add a new option to a choice field in each website.
# Websites are stored in a .csv file

$A = Import-Csv -Path "c:\AllSharePointSites.csv" -Header 'Name', 'URL'

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Define URL and List name variables

$WebURL=$A.URL

$ListName1 ="Internal Documents"
$ListName2 ="Working Documents"
$ListName3 ="Final Documents"

# -->   Update Internal Documents

#Get the Web and Lists objects
$web = Get-SPWeb $WebURL
$List = $web.Lists[$ListName1]

#Add Choices to the field
$ChoiceField = $list.Fields.GetField("Assessment Category") #Get the field
$ChoiceField.Choices.Add("Declaration")

#Commit changes
$ChoiceField.update()


# -->   Update Working Documents

#Get the Web and Lists objects
$web = Get-SPWeb $WebURL
$List = $web.Lists[$ListName2]

#Add Choices to the field
$ChoiceField = $list.Fields.GetField("Assessment Category") #Get the field
$ChoiceField.Choices.Add("Declaration")

#Commit changes
$ChoiceField.update()


# -->   Update Final Documents

#Get the Web and Lists objects
$web = Get-SPWeb $WebURL
$List = $web.Lists[$ListName3]

#Add Choices to the field
$ChoiceField = $list.Fields.GetField("Assessment Category") #Get the field
$ChoiceField.Choices.Add("Declaration")

#Commit changes
$ChoiceField.update()

if anyone could help me with the correct code I would be most grateful.

Ok I tired to run this section of the code, and manually create the string $list and I received the following error:

$ListName1 ="Internal Documents"
$ListName2 ="Working Documents"
$ListName3 ="Final Documents"



# -->   Update Internal Documents

#Get the Web and Lists objects
$web = "http://sp-prod/WorkArea/74643"
Fieldname = "Assesment Category"
$List = "http://sp-prod/WorkArea/74643.lists[Internal Documents].fields.getfield($fieldname)"

write-host $list

#Add Choices to the field
$ChoiceField = $list
$ChoiceField.Choices.Add("Declaration")

#Commit changes
$ChoiceField.update()

Exit

And it produces the following error:

http://sp-prod/WorkArea/74643.lists[Internal Documents].fields.getfield() You cannot call a method on a null-valued expression. At C:\SPS\UpdateChoiceFieldInSites - AddNewChoice1.ps1:31 char:25 + $ChoiceField.Choices.Add <<<< ("Declaration") + CategoryInfo : InvalidOperation: (Add:String) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull

Method invocation failed because [System.String] doesn't contain a method named 'update'. At C:\SPS\UpdateChoiceFieldInSites - AddNewChoice1.ps1:34 char:20 + $ChoiceField.update <<<< () + CategoryInfo : InvalidOperation: (update:String) [], RuntimeException + FullyQualifiedErrorId : MethodNotFound

Can anyone advise why I am getting this?

Thanks, G.

  • Are you getting any error while executing this code? If Yes, add errors in your question. Try to execute script first in development or staging environment, review your updates and if all goes well then run script in production site. – Dikesh Gandhi Jul 12 at 4:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.