Detecting SharePoint environment from an SPFX.
SPFX web parts can be added to modern and classic SharePoint pages and we might need to load different resources depending of the environment type.The environment type can be detected using the EnvironmentType enumeration, directly from your SPFX code.
Member | Description |
---|---|
ClasicSharePoint | Indicates that the framework was loaded from a classic SharePoint page |
Local | Indicates that the framework was loaded from the workbench |
SharePoint | Indicates that the framework was loaded by a client-rendered SharePoint page |
Test | Represents the scenario of the framework hosted in a unit/integration test |
import { Environment, EnvironmentType} from '@microsoft/sp-core-library';
Use the code below to target a specific environment.
if(Environment.type == EnvironmentType.ClassicSharePoint){
//Classic SharePoint page
}else if(Environment.type === EnvironmentType.Local){
//Workbenck page
}else if(Environment.type === EnvironmentType.SharePoint){
//Modern SharePoint page
}else if(Environment.type === EnvironmentType.Test){
//Running on Unit test enveironment
}
Thank you very much for sharing such a useful article. Will definitely saved and revisit your site best MS-101T01: Microsoft 365 Security Management
ReplyDelete