Skip to main content

Update Choice Field values through powerShell

Scenario:

updating choice column option value for all the columns in a site collection including  sub sites.


Solution;
Below script will iterate through all the sites and sub sites of site collection and finds the  choice column "choiceColumnName" and  will change the value "Choice 1" to "Option 1".



Powerhsell Script:
param($url = $(Read-Host -prompt "Root Site Collection Url"))

#Get the PowerShell Snapin

if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null)
{
    Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}


#Get Root Site
$root = Get-SPSite $url

#If site was found / valid
if($root -ne $null)
{

     foreach($subSite in $root.AllWebs)
     {
     
   
       $subSiteTitle = $subSite.Title
                Write-Host $subSiteTitle -ForegroundColor Magenta
                $subSiteURL = $subSite.Url
                Write-Host $subSiteURL -ForegroundColor Cyan
                $tempListArray=$subSite.lists

              foreach($list in $tempListArray)
                             {
                         
                               if( $list.Fields.ContainsField("choiceColumnName") -eq $true)
                               {
                                           $ChoiceField = $list.Fields.GetField("choiceColumnName") #Get the field

                                       
                                         if($ChoiceField.Choices.contains("Choice 1"))
                                          {
  Write-Host "Updating Cloumn:Security Classification" -ForegroundColor Red
 $ChoiceField.Required=$true
                                           $ChoiceField.Choices.Remove("Choice 1")
                                           $ChoiceField.Choices.Add("Option 1")

                                              $ChoiceField.update()
                                              Write-Host "Updated List :" $list.Title
                                              Write-Host "Updated Site " $subSite.Title
                                         break;
                                           }
                                         
                                     

                                     
                                  }    
                             }
       $subSite.Dispose()
      }

        $root.Dispose()

}

Comments

Unknown said…
Excellent! And perfect timing!
Two questions:
Will this work against a doc library of more than 5000 items?
Can this script add a new value as well as changing an existing one?
Thx,
Barry
yes it works with any of the list type irrespective of item limit and also you can add new value

Popular posts from this blog

Download User Profile Pictures from SharePoint 2010 - CSOM

In this post I am trying to explain how we can download UserProfie pictures from SharePoint 2010 on premise environment using CSOM code Two input files: CSV file contains list of users and Email ids(csvInput.csv) Xml input file contains input parameters(InputXml.xml) CSV File format: User ID,First Name,Surname,E-mail, 1233,veera,induvasi,veera@abc.com, 1333,mahesh,gupta,mg@abc.com, 2345,abc,azx,abc@abc.com, InputXml File format: < Element >   < InputDetails >     < siteUrl > http://abc.com </ siteUrl >     < ExportImageLocation > c: \Download </ ExportImageLocation >     < InputCSVFullPath > c:\UserProfile\csvInput .csv </ InputCSVFullPath >     < loginName >create.idea @abc.com </ loginName >     < Password > ##### </ Password >   </ InputDetails > </ Element > Xml parameter Details: < siteUrl> - SharePoint site url < ExportImage

PowerShell script to count number of list and library items in a site for SharePoint Online and MOSS 2007

This below script mainly helps to get the site item count for both SharePoint Online and MOSS 2007. This will be useful when you are migrating site from MOSS2007 to SharePoint online. Script requires input csv file which contains list of site url as below Input.CSV SiteUrl http://testsite1.com http://tetsite.com when you execute the script, you need to provide 2 input parameters 'Enter input csv path with file name'   - c:\script\input.csv Enter Output Path- c:\script\outpu.csv Script for SharePoint 2007 #clear the PowerShell window cls   $CsvPath = Read-Host -Prompt 'Enter input csv path with file name'   $ReportPath = Read-Host -Prompt 'Enter Output Path' # Exclude the following libaries / lists $excludeLists = @( "Master Page Gallery" ,                 "User Information List" ,                 "TaxonomyHiddenList" ,                 "Theme Gallery" ,