Tuesday 15 October 2019

Quick tip - search across sites under a SharePoint hub site

SharePoint Hub Sites are an important aspect of building a modern digital workplace on Office 365. They provide a way of relating sites to each other, and since a modern SharePoint intranet is built from a flat list of sibling site collections which are siblings, such mechanisms are useful to impose a logical structure and coherent user experience. Of course, the idea is that this model deals very well with organizational change, since the association between a site and a hub can be easily updated - if restructuring becomes necessary, it's trivial to update a site to belong to a new hub (something which could not be said of the subsite model heavily used in earlier SharePoint models).

As a reminder, hub sites provide the following:

  • Shared navigation
  • Shared branding 
  • Scoped search (i.e. the ability to search across all sites associated with the hub)
  • A 'top of the tree' destination for the hub

Search and hub sites

There are a few scenarios you might be interested in when it comes to search and hub sites. Everybody's favorite technical SharePoint search person Mikael Svenson (now a part of the search team at Microsoft) has some useful samples at Working with Hub Sites and the search API, including how to use search to list out all your hub sites, including how to filter to return only those with at least one associated site.

However, one thing that's not mentioned there is the fact that you can perform scoped searches for items within all sites associated with a hub. For example, if I have a set of project sites that live under a projects hub, perhaps I'd want to show search results (or provide a custom search page) which restricts result to pages and documents within this project site 'structure'.

This works because Office 365 does something useful here, in that each piece of content that is indexed picks up a tag to indicate which hub it is associated with - in other words, this is propagated down to the item level in SharePoint. The Managed Property 'DepartmentId' is used for this - it contains the ID of the parent hub site.

This is shown in the search query tool below:

This makes sense when you think about it, since Microsoft provide a search across all sites belonging to a hub when you use the search bar in a Hub:


...and the DepartmentId managed property is the mechanism used there.

Sample search queries

Some examples of how you might use this (once you know the ID of the hub you want to scope searches to) might be as follows. NOTE:- in these examples, the GUID specified is the Site ID of the Hub Site in my tenant I want to use. You'd need to drop the appropriate value for your scenario in there:

Show me all pages or documents from any site under this hub:

IsDocument:True AND (DepartmentId:{545621ea-2334-45c2-903b-3b9b93be38ee} OR DepartmentId:545621ea-2334-45c2-903b-3b9b93be38ee)

 Show me news from any site under this hub:

IsDocument:True AND PromotedState:2 AND (DepartmentId:{545621ea-2334-45c2-903b-3b9b93be38ee} OR DepartmentId:545621ea-2334-45c2-903b-3b9b93be38ee)

It seems that sometimes the value can contain braces and sometimes it may not (given how Microsoft themselves search using this property) - so, it may be safer to provide both formats as shown in the queries above.

Search queries used by Microsoft in hub searches

Let's take a closer look at the search terms/clauses used by Microsoft in their native functionality for searching across a hub. Taking a look in browser dev tools shows queries like those shown below (with URLs and IDs from my dev environment shown here).

Full query ("All" tab):

QueryModification: "(marketing) (-ContentClass:ExternalLink AND -FileExtension:vtt AND -Title:OneNote_DeletedPages AND -Title:OneNote_RecycleBin AND -SecondaryFileExtension:onetoc2 AND -ContentClass:STS_List_544 AND -ContentClass:STS_ListItem_544 AND -WebTemplate:SPSPERS AND NOT (ContentClass:STS_Site AND SiteTemplateId:21) AND NOT (ContentClass:STS_Site AND SiteTemplateId:22) AND NOT (ContentClass:STS_List_DocumentLibrary AND SiteTemplateId:21) AND NOT (ContentClass:STS_List_DocumentLibrary AND Author:"system account") AND NOT IndexDocId=17592721738413) AND NOT Path:"https://chrisobriensp-my.sharepoint.com/personal/cob_chrisobrien_com/" AND NOT Path:"https://chrisobriensp-my.sharepoint-df.com/personal/cob_chrisobrien_com/" AND (DepartmentId:{545621ea-2334-45c2-903b-3b9b93be38ee} OR DepartmentId:545621ea-2334-45c2-903b-3b9b93be38ee) -ContentClass=urn:content-class:SPSPeople"

 Full query ("Files" tab):

QueryModification: "(marketing) (DepartmentId:{545621ea-2334-45c2-903b-3b9b93be38ee} OR DepartmentId:545621ea-2334-45c2-903b-3b9b93be38ee) AND (NOT (Title:OneNote_DeletedPages OR Title:OneNote_RecycleBin) AND NOT SecondaryFileExtension:onetoc2 AND NOT FileExtension:vtt AND NOT ContentClass:ExternalLink AND NOT (ContentClass:STS_List_DocumentLibrary AND SiteTemplateId:21) AND NOT ((filetype:aspx OR filetype:htm OR filetype:html OR filetype:mhtml)) AND isDocument:1 OR ((ContentTypeId:0x010100F3754F12A9B6490D9622A01FE9D8F012 OR ContentTypeId:0x0120D520A808*) OR (SecondaryFileExtension:wmv OR SecondaryFileExtension:avi OR SecondaryFileExtension:mpg OR SecondaryFileExtension:asf OR SecondaryFileExtension:mp4 OR SecondaryFileExtension:ogg OR SecondaryFileExtension:ogv OR SecondaryFileExtension:webm OR SecondaryFileExtension:mov)) OR (FileType:ai OR FileType:bmp OR FileType:dib OR FileType:eps OR FileType:gif OR FileType:ico OR FileType:jpeg OR FileType:jpg OR FileType:odg OR FileType:png OR FileType:rle OR FileType:svg OR FileType:tiff OR FileType:webp OR FileType:wmf OR FileType:wpd) OR (ContentTypeId:0x012000*)) AND NOT (ContentTypeId:0x0101009D1CB255DA76424F860D91F20E6C4118* AND PromotedState:2) AND NOT Path:"https://chrisobriensp-my.sharepoint.com/personal/cob_chrisobrien_com/" AND NOT Path:"https://chrisobriensp-my.sharepoint-df.com/personal/cob_chrisobrien_com/" -ContentClass=urn:content-class:SPSPeople"

As you can see, that's a lot of exclusions needed to filter the results down to just the useful ones!

Summary

Hub sites provide a useful construct in Office 365/SharePoint Online, and understanding how to search across all sites within a hub can be a valuable technique when building solutions. The key is to take advantage of the fact that Office 365 puts the parent hub site ID into the 'DepartmentId' managed property on each item in the search index. Knowing that, you can build all sorts of custom solutions around hub sites!