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...
- 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);});})}
Beau Cameron
Jan 06, 2021MVP
LadislavStupak I would use the Search REST API to get all of the site collections.
_api/search/query?querytext='contentclass:sts_site'
LadislavStupak
Jan 06, 2021Brass 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
- LadislavStupakJan 08, 2021Brass 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);});})}- LadislavStupakJan 08, 2021Brass Contributor
This method is better, because in case the restApiUrl contains some additional parameters, the position of Title and path in the cells array can change:
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) => {let cells = result.Cells;console.log("cells: " + JSON.stringify(cells));let title: string = "";let path: string = "";cells.map((cell: any) => {console.log("cell: " + JSON.stringify(cell));let key = cell.Key;if (key == "Title") {title = cell.Value;} else if (key == "Path") {path = cell.Value;}});siteCollections.push({Title: title,SPSiteUrl: path,});});console.log(siteCollections);resolve(siteCollections);});}, (error: any😞 void => {reject("error occured " + error);});})}