Solvedsp dev docs environment missing on WebPartContext for O365 hosted SPFx/workbench

Category

  • Question
  • Typo
  • Bug
  • Enhancement / Suggestion
  • Additional article idea

Expected or Desired Behavior

The environment property on the WebPartPageContext should exist on the O365 hosted SPFx implementation.

Observed Behavior

This property (and many others) is/are missing from SPFx in O365, yet in drop6 they exist in the local copy (& in local workbench).

Steps to Reproduce

When trying to do something such as this:

if (this.context.environment.type === EnvironmentType.Local)

The environment property is undefined when running on O365, but it is defined in the local drop6.

When debugging, here's what you get in O365:
image

But here's what you see in drop6 locally:
image

16 Answers

✔️Accepted Answer

So, we are refactoring APIs for the next drop and the lack of API documentation has lead to this confusion. Apologies for that.

We moved the environment check up a level to the framework and hence it is no longer part of the web part context, but part of the core framework base.

Here is how you will use it now.

Import Environment and EnvironmentType from sp-client-base:

import {
  Environment,
  EnvironmentType
} from '@microsoft/sp-client-base'

Then in your code, you can use the following to check if local or SharePoint, which is the same as earlier except that it is no longer part of the context:

if(Environment.type == EnvironmentType.Local) {
    alert('local');
} else if(Environment.type == EnvironmentType.SharePoint) {
  alert('SharePoint');
}

I have tested this and it works. Please let us know if this works for you.

We are prepping up the API docs, changelogs etc., so it should be easier to track these sort of changes moving forward.

Thanks!

Related Issues:

24
sp dev docs SPFX Field Customizer Extension not loading
I was able to workaround the issue by replacing &loadSPFX=true in the url opened by gulp serve with ...
19
sp dev docs SPFX Local workbench not loading
@nickpape-msft @andrewconnell I found the issue Port 5432 is the default port value used by PostgreS...
12
sp dev docs SPFx Yeoman template broken by TypeScript 2.1 definition in @types/lodash
This is a regression introduced by DefinitelyTyped PR 13994 As a workaround you can run this command...
11
sp dev docs Breaking change on creating content types using CSOM - Value cannot be null
Thanks everyone for confirming that the workaround fix in the PnP PowerShell and NuGet package side ...
3
sp dev docs environment missing on WebPartContext for O365 hosted SPFx/workbench
So we are refactoring APIs for the next drop and the lack of API documentation has lead to this conf...