Pnp-modern-search: Query Template to Limit Scope to Hub Site Associated Sites - Reliably

Created on 9 Feb 2021  路  4Comments  路  Source: microsoft-search/pnp-modern-search

Version used
v4

Describe the bug
As I mentioned in #647, I'm still having trouble using the Hub tokens.

My goal is pretty simple. I want to display items from lists which have a specific set of Content Types (by inheritance) and within a Hub Site scope. Specifically, we've have a list in each associated site which we created using the Work progress tracker template. We want to be able to roll up those tasks based on specific criteria in the filters on a page using the MSWPs.

Using this query in the Query Template works great: we get the items we want - but across all sites.

{searchTerms} ContentTypeId:0x01080091E4FC3986C48A4BA1CC7A34022DAF8B01*

I want to use {Hub.HubSiteId} or {Hub.Id} (not clear what the difference is) as an additional filter, like so:

{searchTerms} DepartmentId:{Hub.HubSiteId} AND ContentTypeId:0x01080091E4FC3986C48A4BA1CC7A34022DAF8B01*

I've tried every combination on the left and right side of that clause I can think of, including RelatedHubSites on the left, as @wobba recommended in the earlier issue.

Of course, now that I've written all this up, I just tried:
{searchTerms} DepartmentId:{\{Hub.HubSiteId}\} AND ContentTypeId:0x01080091E4FC3986C48A4BA1CC7A34022DAF8B01*
which worked.

image

I guess the question then becomes: what is the "best" or "right" way to do this with v4 to make the configuration future proof? What's the difference between {Hub.HubSiteId} and {Hub.Id}?

I'd be game to submit some improvements to the tokens section of the docs if I understood better.

bug documentation

All 4 comments

@sympmarc Agree.. the issue is that DepartmentId requires {} as part of the id (which is someone messing up, but hard to correct due to people take a dependency etc.) I haven't figured out how to escape a { in the query template, so marking this as a bug for v4. I have an interim workaround for you, use RelatedHubSites instead. Works for single hub's, and needed for future connected hubs.

RelatedHubSites:{Hub.Id}

And both tokens return the same value. Maybe @FranckyC knows why we have both?

I tried RelatedHubSites before and couldn't get it to work. This gives me no results:

{searchTerms} RelatedHubSites:{Hub.Id} AND ContentTypeId:0x01080091E4FC3986C48A4BA1CC7A34022DAF8B01*

image

I don't see RelatedHubSites in the Selected properties as an option, either, whereas I do see (and have selected) DepartmentId.

The token calls the REST API /_api/site with select on:

  • Hub.IsHubSite: if the request is performed from a Hub Site, the value is true, else, it is false
  • Hub.HubSiteId: return the GUID of the HUB even if the request is performed from the Hub itself or a joined site. With this information in mind, the recommendation should be to use {Hub.HubSiteId} to scope the results with the DepartmentId. See the example in the documentation that suggests the right way: https://microsoft-search.github.io/pnp-modern-search/usage/search-results/tokens/#site-web-hub-etc-tokens
  • Hub.Id: return the GUID of the current site. If it is the Hub itself, the value will the same as _HubSiteId_, else it match with the current site collection GUID. So, do not use {Hub.Id} and prefer to use {Site.Id} for scope the results with the current site or HubSiteId for scope the results to the Hub site

_(just for the information)_ When you perform a Search with the OOTB SharePoint Search, by default it uses this sentence to scope the results to the hub: AND (DepartmentId:{00000000-0000-0000-0000-000000000000} OR DepartmentId:00000000-0000-0000-0000-000000000000)

@wobba, do you have a link about RelatedHubSites? Because I did not found anything about that yet 馃槅

No links 馃檪 And we don't really document new mp's (not saying we shouldn't). The issue with DepartmentId having {} is an oversight which we now cannot change as people have taken a dep on the format and is why oob queries both to be sure.

You can read this and guess https://docs.microsoft.com/en-us/answers/questions/423254/associated-hubs-in-sharepoint.html

Was this page helpful?
0 / 5 - 0 ratings