Follow Us:
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:
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:
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:
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.
This is an awesome script! Thank you for sharing
The complementary paper includes over 12 years of research, recent survey results, and CRM turnaround success stories.
Request Download
This 60-second assessment is designed to evaluate your organization's collaboration readiness.
Learn how you rank compared to organizations typically in years 1 to 5 of implementation - and which areas to focus on to improve.
This is a sandbox solution which can be activated per site collection to allow you to easily collect feedback from users into a custom Feedback list.
Whether you are upgrading to SharePoint Online, 2010, 2013 or the latest 2016, this checklist contains everything you need to know for a successful transition.