3

I am here today to ask you : how do you retrieve all sites on your tenant by using Pnp (formerly known as pnp-js) ? Or even using CSOM.

I am trying to create a webpart where, in the PropertyPane, the user will be able to search and select the sites on which (s)he wants to search something.

Do you have any idea on how to achieve that ? Because I can't find anything on how to retrieve all sites on a tenant, neither all sites attached to a specific hub.

And yes, I am trying to reproduce Highlighted Content WP Property Pane...See below (translation : first dropdown:"Select sites", search bar : "Search your site by its title", then "0 selected site", and "Frequently used sites", and then a list of my sites on my tenant)

Highlighted Content's PropertyPane

Thanks a lot for your help !

5

Using @pnp/sp framework, you can get the list of all site collections as below:

1) Add the below import statement:

import { sp, SearchQuery, SearchResults} from "@pnp/sp";

2) After that, you can get the data as below:

sp.search(<SearchQuery>{
      Querytext: "contentclass:STS_Site",
      SelectProperties: ["Title", "SPSiteUrl", "WebTemplate"],
      RowLimit: 500,
      TrimDuplicates: false          
    }).then((r: SearchResults) => {

      console.log(r.RowCount);
      console.log(r.PrimarySearchResults);

      r.PrimarySearchResults.forEach((value) => {
         // do your stuff
    });
});

If you want to get all sites associated to a hub using search api, then you can do that as below:

var departmentId = this.context.pageContext.legacyPageContext.departmentId;
// do a null check of department id
sp.search(<SearchQuery>{
      Querytext: `contentclass:STS_Site AND NOT siteid:${departmentId} AND departmentid:${departmentId}`,
      SelectProperties: ["Title", "SPSiteUrl", "WebTemplate"],
      RefinementFilters:[`departmentid:string("{*",linguistics=off)`],
      RowLimit: 500,
      TrimDuplicates: false})
      .then((r: SearchResults) => {

        console.log(r.RowCount);
        console.log(r.PrimarySearchResults);

        r.PrimarySearchResults.forEach((value) => {
           // do your stuff
      });
});

Reference - Working with Hub Sites and Search

  • Thanks @GautamSheth, I will give a try to your solution and come back with feedbacks. Thanks a lot for that documented answer ! – Gaelle Apr 29 at 9:18
3

Do you want all sites in the tenant, or just the sites the user has permissions to? You can use the search API to return a list of webs or site collections the user has access to.

https://yourServer/sites/yourSite/_api/search/query?querytext='contentclass:sts_web'&selectproperties='SiteId,Path,Title'&trimduplicates=true&rowlimit=1000

https://yourServer/sites/yourSite/_api/search/query?querytext='contentclass:sts_site'&selectproperties='SiteId,Path,Title'&trimduplicates=true&rowlimit=1000
  • Thanks @MikeSmith, I will give a try to your solution, even though I don't think i'll have to worry about user's permissions : this webpart will be set by admin or high level user. But that is a good point to keep in mind. Thank you ! – Gaelle Apr 29 at 9:17
0

AAANNDDD found it, thanks to @MikeSmith and @Gautam Sheth, I could make it work. For now, I haven't tested the Hubsites (even if it seems to work).

Here is the query I used to get all sites minus personal sites and minus Appcatalog.

Make sure to try it on SearQueryTool v2 first ! :)

_api/search/query?querytext='(contentclass=STS_Site OR contentclass=STS_Web) (-WebTemplate:SPSPERS AND -SiteTemplate:APPCATALOG)'&SelectProperties='SiteId,WebId,Path,DepartmentId,Title,IsHubSite'&EnableQueryRules=false&RowLimit=1000&Properties='EnableDynamicGroups:true,EnableMultiGeoSearch:true'&sortlist='QLogClicks:Descending'&ClientType='SpClient'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.