powershell_icon

I developed a script the other day that I wanted to share with folks as I couldn’t find anything to do exactly what I wanted. In a SharePoint 2010 site collection, there were approximately 25-30 subsites that most had the same list and columns that we needed to update, but not all the columns were site columns. 

The client had asked me to make a few of the SharePoint 2010 columns required. This would ideally take two minutes if all the columns were site columns, but they weren’t. Since the columns were not inheriting, we needed to turn to our trusted friend
Windows PowerShell

Here are the basic requirements we needed:

  • In a custom list called “Issues” for example, we needed to make 4 fields required.
  • This list exists in approximately 30 subsites in one site collection but not on all subsites.
  • Don’t throw errors - unnecessary errors.

I came up with the following script:

$site = Get-SPsite <site collection URL>
$listname = "name of list" #ENTER THE NAME OF THE LIST THAT WILL BE UPDATED
$fieldstoupdate = @("field1","field2","field3","field4") #ENTER THE LIST OF FIELDS TO BE UPDATED

#ENUMERATE ALL WEBS IN SITE COLLECTION  
foreach ($web in $site.AllWebs) {
    
    #CHECK IF WEB CONTAINS LIST WE WANT TO UPDATE USING NEW METHOD TryGetList()
    $list=$web.Lists.TryGetList($listName)
    if($list -ne $null) {
        Write-host -f black "List"$list.Title "on"$web.Name "at URL"$web.Url
        
        #GET ARRAY OF FIELDS TO UPDATE
        foreach ($field in $fieldstoupdate) {
            
            $fieldname = $list.Fields["$field"]
            $fieldname.required = $true
            $fieldname.update()
            Write-Host -f green "Updated field"$fieldname
        }
    }
    else {
        Write-Host -f red $listName "does not exist in the subsite"$web.Name", skipping"
    } 
}
$web.Dispose()
$site.dispose()


What I did was write a simple few lines to actually do the important part, making the field/column required to ensure that part worked. I wrote a simple little few lines of script that connected to one of the subsites and directly to the list and set the field to be required. Once that was done, I wrote all of the other wrapping and conditional logic. 

Here’s a walkthrough of the logic:

  1. We get an instance of the site collection with Get-SPSite.
  2. We set the name of the list and an array of fields to update to variables.
  3. Enumerate all of the subwebs in the site collection with .allwebs property.
  4. For each subsite in the site collection, we use the TryGetList() method to check if the subsite has the list we identified matching by list name.  This method returns either an instance of the list object if it exists, or $null if it doesn’t exist. 
  5. If the list instance is not $null (lists exists), then we proceed. Otherwise we throw a message that the list doesn’t exist and we exit.
  6. We throw a message to show the list name and subsite name and its URL that we are on.
  7. We enumerate the array of fields we identified, and for each one, set it to be required, and throw a message that its complete for each field.
  8. Dispose of the web and site collection objects in memory.

The TryGetList() method is great because you don’t have to do the normal try / catch exceptions anymore, since it returns $null instead of an exception.  You could just say $list = $web.lists[“list name”], but if it didn’t exist, the script would error. This way, it’s cleaner and smarter.

What it will return will look like this:

PowerShell script

Of course this could be expanded to check if the columns exist on the list, but I knew they did so I didn’t worry about that. Also, you could update any of the field’s properties, not just making it required. Remove the dispose lines, and run “$list | get-member” to see all of the methods and properties available for that list. You could extend it then to update all list items if you needed to be sure that the items have a value for the newly required fields. You would just add another for each loop, like for each ($item in $list.items). 

Happy Scripting! If you'd like more information on C5 Insight's services or solutions, please contact us.