Forum Discussion

CardinalNight's avatar
CardinalNight
Brass Contributor
Oct 04, 2023

How to retrieve all hubsites and associated sites using SPFx and sp/PnP

I have the below code and this retrieves what I believe is all the hubsites in my tenant:

export const GetHubSites = async () => {
  const _sp = getSP();
  const finalArray: any[] = [];
  const hubsites: IHubSiteInfo[] = await _sp.hubSites(); //This seems to return all top level hubsites, but not all child hubsites??.
  console.log(hubsites, 'hubsites');

  await _sp.search({
    Querytext: `contentclass:STS_Site`,
    SelectProperties: ["Title", "SPSiteUrl", "WebTemplate", "SPWebUrl", "SiteName"],
    RefinementFilters: [`departmentid:string("{*",linguistics=off)`],
    RowLimit: 500,
    TrimDuplicates: false,

  })
    .then((r: SearchResults) => {

      console.log(r.RowCount, 'row count');
      console.log(r.PrimarySearchResults, 'r.PrimarySearchResults');
      r.PrimarySearchResults.forEach((value) => {
        finalArray.push(value)
      });
    })
    return finalArray;
}

How would I include all associated sites with this query above. My main aim is to create a grouped list of all hubsites and then expandable/collapsable associated sites.

  • ganeshsanap's avatar
    ganeshsanap
    Oct 05, 2023

    CardinalNight You have to pass the parent hub site ID to DepartmentId property to fetch the associated SharePoint sites with that particular hub site. 

     

    Check the links given in my above response once.


    Please click Mark as Best Response & Like if my post helped you to solve your issue. This will help others to find the correct solution easily. It also closes the item. If the post was useful in other ways, please consider giving it Like.

  • CardinalNight 

     

    You may need to use separate API call for fetching associated sites for each hub site. Check below resources for more information:

    1. @pnp/sp/hubsites 
    2. SPFx: pnp.sp.search how to get all associated sites using hub site name 
    3. REST – Get Associated Sites of a SharePoint Hub Site 

    Please click Mark as Best Response & Like if my post helped you to solve your issue. This will help others to find the correct solution easily. It also closes the item. If the post was useful in other ways, please consider giving it Like.

    • CardinalNight's avatar
      CardinalNight
      Brass Contributor
      Hi Ganesh,
      Thanks for your reply, but I need to understand what filters to use to get hubsites AND their children sites. I understand that it may require a separate API call, but to narrow down my question, what are the different filters to use to get hub sites and their associated child sites?
      E.G I use const url = rootSite + "/_api/search/query?queryText='DepartmentId:{" + hubID + "} contentclass:STS_Site'&trimduplicates=false&selectproperties='Title,Path,DepartmentId,SiteId,IsHubSite'&rowlimit=1000";
      • ganeshsanap's avatar
        ganeshsanap
        MVP

        CardinalNight You have to pass the parent hub site ID to DepartmentId property to fetch the associated SharePoint sites with that particular hub site. 

         

        Check the links given in my above response once.


        Please click Mark as Best Response & Like if my post helped you to solve your issue. This will help others to find the correct solution easily. It also closes the item. If the post was useful in other ways, please consider giving it Like.

Resources