Updating the default search scope for a site collection

I have a scenario where I need to update the default scope for over 300 site collections to reflect a new scope that I have created in Search. When the site collections were created at a previous stage, they were using the “All sites” scope as the default scope.

My first thought was that you should be able to do this in PowerShell, and then my second thought, naturally enough, was has someone else already done this? A quick search brought up the following link, which looked like a good contender, so I quickly cobbled together a script based this.

However, when I ran the script, I ran into a problem on the following line of script:
$group = $scopes.GetDisplayGroup($site.Url, ‘Search Dropdown’)

Which led to the following error message:
“Error Calling GetDisplayGroup with 2 arguments, Specified cast is not valid”.

So after another round of searching based on this error, it was soon apparent I was not the only one who had hit on this error message.

Interestingly, whilst no one is able to definitively state why the problem occurs, it appears that if you browse to the actual scope settings page within the site collection administration before running the script, the error does not happen! Hmm….

However, I have over 300 site collections to update, and I certainly did not want to have to browse to each scope settings page for each of those sites beforehand. Luckily for me, there is another approach that works. Instead of getting a reference to the Search context and then loading the scopes based on that, you should actually invoke the Service context for the site collection, which should be the preferred approach anyway as the Searchcontext is an old SP2007 method, and then get the scopes based on the RemoteScopes object, like so:

$ServiceContext = Get-SPServiceContext -Site $SiteUrl
$Scopes = New-Object Microsoft.Office.Server.Search.Administration.RemoteScopes($ServiceContext)

This method works and allows me to run my script to update all my site collections successfully. I have a link to the script for your consumption here.

Leave a comment