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

C1TonyB 336 Reputation points
2022-04-22T15:24:38.653+00:00

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?

Microsoft 365 and Office | SharePoint Server | Development
Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Emily Du-MSFT 51,851 Reputation points Microsoft External Staff
    2022-04-25T10:41:19.31+00:00

    @C1TonyB

    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 additional answer

Sort by: Most helpful
  1. Limitless Technology 39,931 Reputation points
    2022-04-27T12:17:27.25+00:00

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

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.