-1

Good morning,

I need to create a spfx element to sharepoint 2016. the project dependencies are:

"react": "15.4.2",
"react-dom": "15.4.2",
"@types/react": "0.14.46",
"@types/react-dom": "0.14.18",
"@types/react-addons-shallow-compare": "0.14.17",
"@types/react-addons-update": "0.14.14",
"@types/react-addons-test-utils": "0.14.15",
"@microsoft/sp-core-library": "~1.1.0",
"@microsoft/sp-webpart-base": "~1.1.0",
"@microsoft/sp-lodash-subset": "~1.1.0",
"@microsoft/sp-office-ui-fabric-core": "~1.4.0-0",
"@types/webpack-env": "1.13.1",
"@types/es6-promise": "0.0.33"

How do I get the pageContext and the httpClient?

Thanks

2 Answers 2

1

From anywhere in the Web Part class (for example the render() method), you should be able to access this.context.pageContext and this.context.spHttpClient or this.context.httpClient.

This would be in the /src/webparts/helloWorld/HelloWorldWebPart.ts file. (Replace HelloWorld with whatever you named your web part.)

2
  • Hi, thanks for your response. in my sharepoint 2016 environment this.context is an empty object {} Commented Mar 11, 2019 at 15:15
  • Is the web part added to a page? Could you paste the code you are using to access this.context? I don't have a working SP2016 dev environment to test it myself unfortunately. As an alternative for context, you could access the global variable _spPageContextInfo to view the context information. I'm not so sure about an alternative for httpClient - worst case, you could go with vanilla fetch or jQuery.ajax to make those requests.
    – Richard Li
    Commented Mar 11, 2019 at 21:24
0

Look at the images to see how to get and send pageContext and spHttpClient to you components.
enter image description here

your props should look like this:

enter image description here

Last step is to get them in your component where all code is by just typing this.props.pageContext & this.props.spHttpClient under your render() method.

1
  • Hi, thanks for your response. in my sharepoint 2016 environment this.context is an empty object {} Commented Mar 11, 2019 at 15:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.