Working Amongst Hub Sites Too The Search Api

By Tuesday, April 10, 2018 on April 10, 2018 Tags:
Working Amongst Hub Sites Too The Search Api.


Photo past times Sunaina Kamal at Unsplash.

Hub sites are a overnice way to practise a virtual ii degree site hierarchy without using sub sites. An administrator volition nominate a site every bit a hub site, in addition to and so other sites tin flaming associate themselves to this hub. By associating a site to a hub the site volition inherit the navigation in addition to the visual await of the hub itself, ensuring a consisting await in addition to feel. Also items from associated sites volition surface inward search at the hub degree (as long every bit you lot accept read access to the item).

Hub sites are a overnice way to practise a virtual ii degree site hierarchy without using sub si Working alongside Hub Sites in addition to the search API

Note: The hub site characteristic is currently inward preview in addition to alone available for target free tenant at the fourth dimension of writing.

After playing alongside hub sites you lot mightiness desire your favorite developer to practise something for your hub sites, in addition to this is where the search API is handy.

Search schema

At the optic of hub sites inward regards to search is the managed belongings named DepartmentId which is the site id of the hub site. Both the hub site in addition to sites associated to the hub site accept the same id inward this property. There is also a managed belongings named IsHubSite which currently has no value, so you lot cannot purpose this to listing the hub sites themselves.

Instead of explaining it all I’ll give around sample queries on how you lot tin flaming purpose search to enquiry for hub sites.

List all sites which is either a hub site or associated alongside a hub site

Query: contentclass=sts_site
Refinement filter: departmentid:string("{*",linguistics=off)
Trim duplicates: simulated
REST: /_api/search/query?querytext='contentclass%3dsts_site'&refinementfilters='departmentid:string("{*"%2clinguistics%3doff)'&trimduplicates=false

If you lot page through all the results you lot volition larn all sites either beingness a hub site or beingness associated to a hub site. For the curious ones the refinement filter is FQL in addition to at the optic of the query. This slice of FQL limits results where in that place truly is a value inward the DepartmentId managed property.

List all hub site id’s

Query: contentclass=sts_site
Refinement filter: departmentid:string("{*",linguistics=off)
Trim duplicates: simulated
Collapse Specification: departmentid:1
REST: /_api/search/query?querytext='contentclass%3dsts_site'&refinementfilters='departmentid:string("{*"%2clinguistics%3doff)'&collapsespecification='departmentid:1'&trimduplicates=false

This volition yield i lawsuit per hub site id due to the collapse specification, exactly the i lawsuit beingness returned per hub site id could real good survive a site associated to the hub site in addition to non the hub site itself.

For results where the SiteId is non equal to DepartmentId, you lot take to perform i to a greater extent than telephone phone to fetch the actual hub site alongside a enquiry similar to: ContentClass=sts_site SiteId:"<DepartmentId>". Be certain to purpose : in addition to non =, every bit equals won’t tally on the site id alongside a guid.

List all hub site id’s having at to the lowest degree i site associated alongside it

Query: contentclass=sts_site
Refinement filter: departmentid:string("{*",linguistics=off)
Trim duplicates: simulated
Row limit: 0
Refiners: departmentid(filter=1000/2/*)

For this enquiry you lot take to await at the refinement results, in addition to non the listing itself, which is why I laid upward row bound to zero. The filter volition supply alone items where the refiner count is 2 or higher, therefore eliminating orphaned hub sites.

At the writing of this ship service the max number of hub sites is 50, so you lot won’t meet an number alongside likewise many refiner values, which way you lot tin flaming purpose l instead of k inward the filter – exactly amend to survive futurity proof.

If you lot take away the filter part, you lot tin flaming purpose this enquiry to fetch all hub site id’s, similar to the get-go sample, except using refinement results instead.

Hub sites are a overnice way to practise a virtual ii degree site hierarchy without using sub si Working alongside Hub Sites in addition to the search API

PnP PowerShell code to fetch all hub sites, in addition to whatsoever associated site, in addition to listing them past times the hub site

For this i I volition fetch all results, using paging if needed (the -All parameter), in addition to and so iterate over in addition to grouping them past times the hub site. Similar code should survive pretty slowly to write using both REST, CSOM in addition to JSOM.

# List all sites beingness a hub site or associate to a hub site $results = Submit-PnPSearchQuery -Query 'contentclass=sts_site' -RefinementFilters 'departmentid:string("{*",linguistics=off)' -TrimDuplicates $false -SelectProperties @("Title","Path","DepartmentId","SiteId") -All -RelevantResults  # Filter out the hub sites $hubSites = $results |? { $_.DepartmentId.Trim('{','}') -eq $_.SiteId  }  # Loop over the hub sites foreach( $hub inward $hubSites ) {     Write-Host $hub.Title - $hub.Path -ForegroundColor Green     # Filter out sites associated to the electrical flow hub     $associatedSites = ($results |? { $_.DepartmentId -eq $hub.DepartmentId -and $_.SiteId -ne $hub.SiteId })     foreach($site inward $associatedSites) {         Write-Host "`t"$site.Title - $site.Path -ForegroundColor Yellow     } }  

Other options

There are other options to function alongside subsites. The CSOM Site object contains a belongings named IsHubSite you lot tin flaming cheque on, every bit does the tenant properties of a site. The site object also has a HubSiteId belongings which corresponds to the search managed belongings DepartmentId.

Thanks for reading : Working Amongst Hub Sites Too The Search Api