Sunday 24 January 2016

SharePoint 2013 Powershell script to update document library column (choice field category) when any folder created in Document Library

Scenario : My client wants to update a document library column(selected as choice category)  whenever a folder is added in document library he wants that folder name should be added in category menu also.

Below is the tested SharePoint 2013 script
--------------------------------------------------------------------------------------------------------

Add-PsSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

$web=Get-spweb "Your site collection"

$list=$web.Lists.TryGetList("Your document library")

$i=0
$RootFolder=$web.GetFolder("Your document library")

foreach($folder in $RootFolder.SubFolders)
{

$ChoiceField = $list.Fields.GetField("Your column where category filed needs to be added") #Get the field
    if($folder.Name -ne "Forms")
      {
     
        $fname=  $folder.Name
     
       
if($ChoiceField.Choices.Contains($fname))
{
#write-host $ChoiceField.Choices.Item($i)
}
else
        {
       
        $ChoiceField.Choices.Add($fname)
        }

      }
      $i=$i+1
     
$ChoiceField.update()

}
----------------------------------------------------------------------------------------------------------------------

No comments:

Post a Comment