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.

18 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
  2. Nikhil said:

    Hello David,

    Thank you for the great post, it helped a lot !

    I was wondering if this can be possible to access only two sites instead of accessing the entire sharepoint site pages. As a hub site from here i want to access only two sites(tenant sites)?

    Thanks and Regards,
    Nikhil

    July 19, 2019
    Reply
    • David Ramalho said:

      Hi Nikhil,

      I don’t think is possible to have the search on SharePoint to search only on the site (Site A) and another site (Site B). You need to select the search global (A, B, C, etc) when searching or just on the site (A).

      July 20, 2019
      Reply
      • Nikhil said:

        Thank you for the reply, for example I have requirement like I have hub site underneath that I have A,B,C sites. As a requirement I need to search only A and B but not C. As per your comments it is possible only by global search which will search entire share point ?

        July 20, 2019
        Reply
        • David Ramalho said:

          Yes, that is correct.

          July 21, 2019
          Reply
          • Nikhil Prakash Muthya said:

            Hello David,

            Well i come across with another requirement, as i would need some suggestion. I was wondering if this can be possible to enable the search to modern page (.aspx). Basically i need to have something like this “Search this site” to “Search this page” only current page.
            Using pnp PS.

            Thanks and Regards,
            Nikhil

            August 28, 2019
          • David Ramalho said:

            Hi Nikhil,

            I don’t think is possible to search on the context of the page using the regular search. I would recommend creating multiple list views to allow your users to filter quickly.

            My best,
            David Ramalho

            September 1, 2019
  3. Nikhil Prakash Muthya said:

    Hello David,

    Thank you for the reply, I was wondering something like this. So for Document-Library when we search “Documents”- Results to files and folders, it can be searched same way when we navigate to “Site Pages” in modern site. You will have Search option on top of the navigation menu, where you could possibly search the (.aspx) pages. But unfortunately for some users it disappeared, do you have any idea why it is not visible for some users and some can see.

    Thanks and Regards,
    Nikhil

    September 4, 2019
    Reply
    • David Ramalho said:

      Hi Nikhil,

      The Microsft Search could be the reason for some sites don’t have the Search. If you’re on Modern it’ll display this Seach on the Suite Bar but on the classic SharePoint uses the default search.
      Thanks and let me know if this helped.

      September 17, 2019
      Reply
  4. Alan said:

    Hi David,
    Thanks for sharing this article. Great information!
    I was wondering if there is a way to make the Microsoft Search bar within an associated Hub Site default to the top level hub site. Currently if you Search from an associated site you only shown the results for that associated site collection and if you require results for all associated sites in the Hub you need to use the breadcrumb trail to navigate to the Hub site. Is there a way to set the Hub site as the default when searching from all associated sites?
    Thanks again! 🙂

    September 16, 2020
    Reply
  5. Nikhil said:

    Hello David,

    When we activate the global search to one of the site collection, how can we identify it has changed. From site collection admin in settings or any settings to identify the change.

    I know i can search anything on search engine.

    And is there any way to activate for Hubsite like https://sharepoint-tricks.com/activate-global-search-for-modern-site/ easier way or i have to use the same approach like above for hubsite.

    September 18, 2020
    Reply
  6. Nikhil said:

    I mean i would like to activate on only to hubsite, because as i see that above scripts activates to all the associated sites.

    But i only to need to activate to my hubsites.

    September 18, 2020
    Reply
    • David Ramalho said:

      Hi,

      You could just connect to the Root hub site and use the below command to apply the scope search for that site.
      If you remove the $web.SearchScope = 1 to $web.SearchScope, if will give you the option activated for the site.

      My best,
      David Ramalho

      September 18, 2020
      Reply
      • Nikhil said:

        Hello David,

        You mean $web.SearchScope -> this will only enable the global search for hubsite only not associated sites.

        If i follow above procedure, i have to see the changes as below ?

        1. – Global Search (Hubsite)
        1.1 – All sites in this Hub or default behavior (Associated site from hub site)
        1.2 – All sites in this Hub or default behavior (Associated site from hub site)
        1.3 – All sites in this Hub or default behavior (Associated site from hub site)

        Thanks,
        Nikhil

        September 18, 2020
        Reply

Leave a Reply

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