/***** This is Webpart.tsx file *****/
import * as React from 'react';
import { SPHttpClient, SPHttpClientResponse } from '@microsoft/sp-http';
import { WebPartContext } from '@microsoft/sp-webpart-base';
/*** use the context in this file ****/
export interface IExpProps {
context: WebPartContext;
}
export default class Exp extends React.Component<IExpProps, any> {
constructor(props: any) {
super(props)
this.state = {
Sites: [],
}
}
componentDidMount(): void {
debugger;
const webUrl: string = this.props.context.pageContext.site.absoluteUrl
const requestUrl = `${webUrl}/_api/search/query?querytext='contentclass:STS_Site contentclass:STS_Web'&selectproperties='Title,Path'&rowlimit=500
`;
let siteUrl :any=[]
this.props.context.spHttpClient.get(requestUrl, SPHttpClient.configurations.v1)
.then((response: SPHttpClientResponse) => {
if (response.ok) {
response.json().then((data) => {
const sites: any[] = data.PrimaryQueryResult.RelevantResults.Table.Rows
if (sites){
for (let i = 0; i <= sites.length; i++) {
const url=sites[i].Cells.find((cell: any) => cell.Key === 'Path').Value
console.log("sites and Subsites",url)
siteUrl.push({
Url:url
})
this.setState({Sites:siteUrl})
}
}
console.log(this.state.Sites)
});
} else {
console.log(`Error: ${response.statusText}`);
}
})
.catch((error: any) => {
console.log(`Error: ${error}`);
});
}
public render(): React.ReactElement<IExpProps> {
return (
<div>Sites:
<ol>
{this.state.Sites.map((items:any) => {
return [
<li>{items.Url}</li>
]
})}
</ol>
</div>
);
}
}
/******* This is webpart.ts file *****/
import * as React from 'react';
import * as ReactDom from 'react-dom';
import { Version } from '@microsoft/sp-core-library';
import {
IPropertyPaneConfiguration,
PropertyPaneTextField
} from '@microsoft/sp-property-pane';
import { BaseClientSideWebPart } from '@microsoft/sp-webpart-base';
import { IReadonlyTheme } from '@microsoft/sp-component-base';
import * as strings from 'ExpWebPartStrings';
import Exp from './components/Exp';
import { IExpProps } from './components/IExpProps';
export interface IExpWebPartProps {
description: string;
}
export default class ExpWebPart extends BaseClientSideWebPart<IExpWebPartProps> {
private _isDarkTheme: boolean = false;
private _environmentMessage: string = '';
public render(): void {
const element: React.ReactElement<IExpProps> = React.createElement(
Exp,
{
description: this.properties.description,
isDarkTheme: this._isDarkTheme,
environmentMessage: this._environmentMessage,
hasTeamsContext: !!this.context.sdks.microsoftTeams,
userDisplayName: this.context.pageContext.user.displayName,
context:this.context /**** Mention the context here ***/
}
);
ReactDom.render(element, this.domElement);
}
protected onInit(): Promise<void> {
return this._getEnvironmentMessage().then(message => {
this._environmentMessage = message;
});
}
private _getEnvironmentMessage(): Promise<string> {
if (!!this.context.sdks.microsoftTeams) { // running in Teams, office.com or Outlook
return this.context.sdks.microsoftTeams.teamsJs.app.getContext()
.then(context => {
let environmentMessage: string = '';
switch (context.app.host.name) {
case 'Office': // running in Office
environmentMessage = this.context.isServedFromLocalhost ? strings.AppLocalEnvironmentOffice : strings.AppOfficeEnvironment;
break;
case 'Outlook': // running in Outlook
environmentMessage = this.context.isServedFromLocalhost ? strings.AppLocalEnvironmentOutlook : strings.AppOutlookEnvironment;
break;
case 'Teams': // running in Teams
environmentMessage = this.context.isServedFromLocalhost ? strings.AppLocalEnvironmentTeams : strings.AppTeamsTabEnvironment;
break;
default:
throw new Error('Unknown host');
}
return environmentMessage;
});
}
return Promise.resolve(this.context.isServedFromLocalhost ? strings.AppLocalEnvironmentSharePoint : strings.AppSharePointEnvironment);
}
protected onThemeChanged(currentTheme: IReadonlyTheme | undefined): void {
if (!currentTheme) {
return;
}
this._isDarkTheme = !!currentTheme.isInverted;
const {
semanticColors
} = currentTheme;
if (semanticColors) {
this.domElement.style.setProperty('--bodyText', semanticColors.bodyText || null);
this.domElement.style.setProperty('--link', semanticColors.link || null);
this.domElement.style.setProperty('--linkHovered', semanticColors.linkHovered || null);
}
}
protected onDispose(): void {
ReactDom.unmountComponentAtNode(this.domElement);
}
protected get dataVersion(): Version {
return Version.parse('1.0');
}
protected getPropertyPaneConfiguration(): IPropertyPaneConfiguration {
return {
pages: [
{
header: {
description: strings.PropertyPaneDescription
},
groups: [
{
groupName: strings.BasicGroupName,
groupFields: [
PropertyPaneTextField('description', {
label: strings.DescriptionFieldLabel
})
]
}
]
}
]
};
}
}
/******* This is Props.ts file *****/
import { WebPartContext } from "@microsoft/sp-webpart-base";
export interface IExpProps {
description: string;
isDarkTheme: boolean;
environmentMessage: string;
hasTeamsContext: boolean;
userDisplayName: string;
context:WebPartContext; /**** Declare here context *****/
}