Activate SharePoint Global Search on Hub sites

The Hub sites that are connected on SharePoint (hubify), by default will have the search made on the particular site that the users connects. Making the search on those SharePoint sites to be global can make the life easier of your employees to find documents on the Intranet. On this article, we’ll provide a PowerShell script that makes all the sites of that particular Hub to have the Search globally activated.

For now, to activate this feature you need to run this PowerShell script, however, Microsoft will provide a way to set this setting using the UI later this year. It’s possible they provide this option per site. If you don’t have permissions, it’ll show the sites that you didn’t have permissions to activate this feature. You can then send them the PowerShell to activate on that particular site. You can check that script on the next link.

cls
$tenantAdmin = "https://contoso-admin.sharepoint.com"
$hubSite = "https://contoso.sharepoint.com/sites/contoso"

try{
 Connect-PnPOnline -Url $tenantAdmin -UseWebLogin
} catch {
 Write-Host "Unable to connect."
 exit
}
 
$HubSite = Get-PnPHubSite $hubSite
$HubSiteId = $HubSite.SiteId
$ModernSites = (Get-PnPTenantSite -Template 'GROUP#0') + (Get-PnPTenantSite -Template 'SITEPAGEPUBLISHING#0')
$SitesFromHub = New-Object System.Collections.ArrayList
 
Write-Host ("Searching {0} sites:" -f $HubSite.Title) -BackgroundColor Gray
 foreach ($ModernSite in $ModernSites){
  $site = Get-PnPHubSite $ModernSite.Url
  if($site.SiteUrl){			
   if($site.SiteId -eq $HubSiteId){
    Write-Host ("* {0} - {1}" -f $ModernSite.Title, $ModernSite.Url)
    $SitesFromHub.Add($ModernSite) | Out-Null
  }
 }
}
 
Write-Host ""
Write-Host "Activating search global at:" -BackgroundColor Gray
foreach ($SiteHub in $SitesFromHub){
  Write-Host ("* {0} - {1} ... " -f $SiteHub.Title, $SiteHub.Url) -NoNewline	
  Connect-PnPOnline -Url $SiteHub.Url -UseWebLogin
 
 try{
  $web = Get-PnPWeb
  #1 to activate Global Search and 0 to deactivate Global Search
  $web.SearchScope = 1
  $web.Update()
  Invoke-PnPQuery
  Write-Host "Done" -BackgroundColor Green	
  }
 catch{
   Write-Host "No permission" $site.url -BackgroundColor Red	
  }
}
Write-Host "All Done"
Write-Host "Press any key to Close..."
$Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

Conclusion

Regardless of the site where you’re user is connected, they will have access to all documents stored on intranet on the search area and avoid to go on a specific site to search a document.

3 Comments

  1. HLJ said:

    Hello David,
    This is a great post and script.
    But wondering, if I go the the search window and then click SITES – Search for NAME OF SITE / DOCUMENT / CONTENT – in all content doesn’t that do the same thing? I was able to find the content I wanted. Although granted it is about 2 additional clicks.

    May 8, 2019
    Reply
    • David Ramalho said:

      Hello,

      The idea is to make easier your users find the specific content and this configuration can help on that task. This will come to the point where you need to decide which option between site and global search is better for the end-user.

      Thank you for your feedback and have a great day!!

      May 13, 2019
      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *