Forum Discussion
CardinalNight
Mar 27, 2024Brass Contributor
How do I use sp/pnp sp.search to find all Associated sites when querying a hub site Id
I'm making an app which lists all hub sites on an SP tenant. I want a user to click on a hub site and it shows all associated sites.
I cannot figure out how to use [sp.search][1]
[1]: https://pnp.github.io/pnpjs/sp/search/
The documentation is quite assumptive. Here is what I've attempted:
export const GetAssociatedSites = async (hubId: string | undefined) => {
const _sp = getSP();
const finalArray: any[] = [];
await _sp.search({
StartRow: 0,
QueryTemplate: "contentclass: STS_Site AND DepartmentId:1111*",
Querytext: 'contentclass:STS_Site contentclass:STS_Web',
//SiteTitle:{searchTerms} AND contentclass:STS_site AND DepartmentID:{{PageContext.hubSiteId}}
SelectProperties: [
"Title",
"SPSiteUrl",
"WebTemplate",
"SPWebUrl",
"SiteName",
"RelatedHubsites",
"IsHubSite",
"ParentLink",
"Department",
"Path"
],
RefinementFilters: [`departmentid:string("${hubId}",linguistics=off)`],
RowLimit: 500,
TrimDuplicates: false,
})
.then((r: SearchResults) => {
r.PrimarySearchResults.forEach((value) => {
finalArray.push(value)
});
currentResults = r
})
console.log(currentResults, 'currentResults')
return currentResults;
}
I get an empty array back. Please help.
I managed to get some results using:
export const GetChildSites = async (hubId: string | undefined): Promise<SearchResults> => {
console.log(hubId, 'hubId in data')
const _sp = getSP();
const bracketHubId = hubId;
await _sp.search({
StartRow: 0,
Querytext: '(contentclass:"STS_Site") (contentclass:"STS_Web")',
QueryTemplate: `contentclass: STS_site AND DepartmentId:${bracketHubId}`,
SelectProperties: [
"Title",
"SPSiteUrl",
"WebTemplate",
"WebId",
"SPWebUrl",
"SiteName",
"RelatedHubsites",
"IsHubSite",
"ParentLink",
"Department",
"DepartmentId",
"Path"
],
RowLimit: 500,
TrimDuplicates: false,
})
.then((r: SearchResults) => {
currentResults = r
})
return currentResults;
}
But some hubsites will not return any associated sites, whereas some do. I've checked with admin portal to see if these hubsites have associated sites any they do.....What's wrong here?
Hello CardinalNight
maybe this parameters can help you: https://microsoft-search.github.io/pnp-modern-search/scenarios/Connect-to-a-search-results-webpart/
Best, Dave
- CardinalNightBrass ContributorThanks DaveMehr, that webpart just doesn't return any hubsites for me.