Question

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 !

Était-ce utile?

La solution 3

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'

Autres conseils

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({ 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;
// Be sure to wrap departmentId in actual string {}... but NOT siteid
// 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

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
Licencié sous: CC-BY-SA avec attribution
Non affilié à sharepoint.stackexchange
scroll top