Forum Discussion

LadislavStupak's avatar
LadislavStupak
Brass Contributor
Jan 06, 2021
Solved

SPFx - Get all site collections

Dear Ladies and Gentlemen,

 

how can I get all site collections, for which the logged in user has access rights, through SharePoint Framework, which works in Office 365 and in SharePoint on premise?

 

Thank you very much for your help.

 

With best regards

Ladislav Stupak

  • LadislavStupak's avatar
    LadislavStupak
    Jan 08, 2021

    This method is working well:

     

    public GetAllSiteCollections5(context: WebPartContext๐Ÿ˜ž Promise<ISiteCollection[]> {
            
            let restApiUrl: string = context.pageContext.web.absoluteUrl + "/_api/search/query?querytext='contentclass:sts_site'";

            let siteCollections: ISiteCollection[] = [];

            let config: SPHttpClientConfiguration = new SPHttpClientConfiguration({
                defaultODataVersion: ODataVersion.v3
            });

            return new Promise<ISiteCollection[]>(async(resolve, reject) => {

                context.spHttpClient
                .get(restApiUrl, config, {headers: { Accept: "application/json;odata=minimalmetadata;charset=utf-8"}})
                .then((response: SPHttpClientResponse) => {

                    console.log("response contains: " + JSON.stringify(response));

                    response.json().then((results: any) => {

                        console.log(restApiUrl);
                        console.log("results contains: " + JSON.stringify(results));
                        
                        let resultsList = results.PrimaryQueryResult.RelevantResults.Table.Rows;
                        
                        console.log(resultsList);
                        
                        resultsList.map((result: any) => {

                            console.log(result);

                            let cell3 = result.Cells[3];
                            console.log("cell3: " + JSON.stringify(cell3));

                            let cell6 = result.Cells[6];
                            console.log("cell6: " + JSON.stringify(cell6));

                            let cell3Value = cell3.Value;
                            console.log(cell3Value);

                            let cell6Value = cell6.Value;
                            console.log(cell6Value);

                            siteCollections.push({
                                Title: cell3Value,
                                SPSiteUrl: cell6Value,
                            });
                        });

                        console.log(siteCollections);
                        resolve(siteCollections);
                    });
                }, (error: any๐Ÿ˜ž void => {

                    reject("error occured " + error);
                });
            })
        }

4 Replies

    • LadislavStupak's avatar
      LadislavStupak
      Brass Contributor

      Beau Cameron thank you for your answer.

       

      When I do it through the following method below, then I receive following error: "Microsoft.SharePoint.Client.UnknownError

       

      public GetAllSiteCollections2(context: WebPartContext๐Ÿ˜ž Promise<ISiteCollection[]> {

              let restApiUrl: string = context.pageContext.web.absoluteUrl + "/_api/search/query?querytext='contentclass:sts_site'";        
              
              let siteCollections: ISiteCollection[] = [];

              return new Promise<ISiteCollection[]>(async(resolve, reject) => {

                  context.spHttpClient
                      .get(restApiUrl, SPHttpClient.configurations.v1)
                      .then((response: SPHttpClientResponse) => {
                          response.json().then((results: any) => {

                              console.log(restApiUrl);
                              console.log(results);

                              results.value.map((result: any) => {

                                  siteCollections.push({
                                      Title: result.Title,
                                      SPSiteUrl: result.Url,
                                  });
                              });

                              console.log(siteCollections);
                              resolve(siteCollections);                    
                          });
                      }, (error: any๐Ÿ˜ž void => {

                          reject("error occured " + error);
                      });
              })
          }
       
      When I try it through following method, where I override the configuration version, then I receive as results an empty array. See print screen with the results log below the method.
       
      public GetAllSiteCollections(context: WebPartContext๐Ÿ˜ž Promise<ISiteCollection[]> {

              let restApiUrl: string = context.pageContext.web.absoluteUrl + "/_api/search/query?querytext='contentclass:sts_site'";
                            
              const spSearchConfig: ISPHttpClientConfiguration = {

                  defaultODataVersion: ODataVersion.v3
              };

              let siteCollections: ISiteCollection[] = [];

              return new Promise<ISiteCollection[]>(async(resolve, reject) => {

                  context.spHttpClient
                      .get(restApiUrl, SPHttpClient.configurations.v1.overrideWith(spSearchConfig))
                      .then((response: SPHttpClientResponse) => {
                          response.json().then((results: any) => {

                              console.log(restApiUrl);
                              console.log(results);
                             
                              results.value.map((result: any) => {

                                  siteCollections.push({
                                      Title: result.Title,
                                      SPSiteUrl: result.Url,
                                  });
                              });

                              console.log(siteCollections);
                              resolve(siteCollections);                    
                          });
                      }, (error: any๐Ÿ˜ž void => {

                          reject("error occured " + error);
                      });
              })
          }
       
       
      Thank you for help.
       
      With best regards
      Ladislav Stupak
      • LadislavStupak's avatar
        LadislavStupak
        Brass Contributor

        This method is working well:

         

        public GetAllSiteCollections5(context: WebPartContext๐Ÿ˜ž Promise<ISiteCollection[]> {
                
                let restApiUrl: string = context.pageContext.web.absoluteUrl + "/_api/search/query?querytext='contentclass:sts_site'";

                let siteCollections: ISiteCollection[] = [];

                let config: SPHttpClientConfiguration = new SPHttpClientConfiguration({
                    defaultODataVersion: ODataVersion.v3
                });

                return new Promise<ISiteCollection[]>(async(resolve, reject) => {

                    context.spHttpClient
                    .get(restApiUrl, config, {headers: { Accept: "application/json;odata=minimalmetadata;charset=utf-8"}})
                    .then((response: SPHttpClientResponse) => {

                        console.log("response contains: " + JSON.stringify(response));

                        response.json().then((results: any) => {

                            console.log(restApiUrl);
                            console.log("results contains: " + JSON.stringify(results));
                            
                            let resultsList = results.PrimaryQueryResult.RelevantResults.Table.Rows;
                            
                            console.log(resultsList);
                            
                            resultsList.map((result: any) => {

                                console.log(result);

                                let cell3 = result.Cells[3];
                                console.log("cell3: " + JSON.stringify(cell3));

                                let cell6 = result.Cells[6];
                                console.log("cell6: " + JSON.stringify(cell6));

                                let cell3Value = cell3.Value;
                                console.log(cell3Value);

                                let cell6Value = cell6.Value;
                                console.log(cell6Value);

                                siteCollections.push({
                                    Title: cell3Value,
                                    SPSiteUrl: cell6Value,
                                });
                            });

                            console.log(siteCollections);
                            resolve(siteCollections);
                        });
                    }, (error: any๐Ÿ˜ž void => {

                        reject("error occured " + error);
                    });
                })
            }

Resources