Forum Discussion
LadislavStupak
Jan 06, 2021Brass Contributor
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
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 I would use the Search REST API to get all of the site collections.
_api/search/query?querytext='contentclass:sts_site'- LadislavStupakBrass 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 regardsLadislav Stupak- LadislavStupakBrass 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);});})}