question

TonyB-9562 avatar image
0 Votes"
TonyB-9562 asked LimitlessTechnology-2700 answered

How do you update SharePoint 2016 On Premise SubSite Choice Fields using Powershell?

I have a Site Collection with many subsites that all have the same Custom List (CustomList1) and Choice Column (ChoiceColumn1 - List Columns not Site Columns). I successfully added a field value to ChoiceColumn1 under CustomList1 for the Site Collection but the update fails for SubSites under the Site Collection URL.

Code:

Add-PsSnapin Microsoft.SharePoint.PowerShell -erroraction silentlycontinue

$web = get-spweb https://webapp.domain.com/sites/sitecollection/
$list = $web.lists[“CustomList1”]
$choice = $list.fields[“ChoiceColumn1”]
$choice.choices
$choice.choices.add(“New Value”)
$choice.update()

If I change:

$web = get-spweb https://webapp.domain.com/sites/sitecollection/

To

$web = get-spweb https://webapp.domain.com/sites/sitecollection/subsite

It fails.

Error:

Cannot Index into a null value.
Exception calling "Update" with "0" argument(s): "The node to be inserted is from a different document context."

How can I update the Choice Column field value for all subsites under the Site Collection?

windows-server-powershelloffice-sharepoint-server-development
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

EmilyDu-MSFT avatar image
0 Votes"
EmilyDu-MSFT answered TonyB-9562 commented

@TonyB-9562

Try below PowerShell.

 $web = Get-SPWeb -site https://webapp.domain.com/sites/sitecollection
    
 foreach($web in $web.Webs){
   $lists = $web.Lists | Where-Object { $_.Title -eq 'CustomList1' }
   $choice = $lists.fields | Where-Object { $_.Title -eq 'ChoiceColumn1' }    
   $choice.choices
   $choice.choices.add("New Value")
   $choice.update() 
 }

If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you. Your PS Script worked.

0 Votes 0 ·
LimitlessTechnology-2700 avatar image
0 Votes"
LimitlessTechnology-2700 answered

Hello TonyB,

I would recommend to use SPField.ShowInNewForm and SPField.ShowInEditForm properties to control the visibility of fields in form pages, as example:


$web = Get-SPWeb <yoururl>
$list = $web.Lists["CustomList1"]
$field = $list.Fields["ChoiceColumn1"]
$field.ShowInNewForm = $False
$field.ShowInEditForm = $True
$field.Update()
$choice = $list.fields[“ChoiceColumn1”]
$choice.choices
$choice.choices.add(“New Value”)
$choice.update()
$list.Update()



--If the reply is helpful, please Upvote and Accept as answer--

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.