Forum Discussion
Mike Jansen
Oct 11, 2017Iron Contributor
SPFX > pnp.sp reference in React project
I have a working SPFX project using pnp.sp.search (without an additional javascript framework). I import pnp.sp like this in my webparts.ts file : import * as pnp from 'sp-pnp-js';
import { SearchQ...
Mike Jansen
Oct 11, 2017Iron Contributor
Hi Maggan Wåhlin.
Yes I did. Double checked it in Visual Studio Code. Sp.pnp.js is under "node_modules"
Thanks, Mike
Maggan Wåhlin
Oct 11, 2017Iron Contributor
I reference the package this way:
import { default as pnp, ItemAddResult, Web } from "sp-pnp-js";
import { default as pnp, ItemAddResult, Web } from "sp-pnp-js";
- Mike JansenOct 11, 2017Iron Contributor
Tried that but no luck sofar. Maybe my code is wrong in some way. I just copied it from my working project. Like this:
import { default as pnp, ItemAddResult, Web } from "sp-pnp-js"; import { SearchQuery, SearchResults } from "sp-pnp-js"; import * as React from 'react'; import styles from './Reactsearch.module.scss'; import { IReactsearchProps } from './IReactsearchProps'; import { escape } from '@microsoft/sp-lodash-subset'; export default class Reactsearch extends React.Component<IReactsearchProps, {}> { private GetSearchresults(): void { pnp.sp.search(<SearchQuery>{ Querytext: "ContentType:TEST_matters", RowLimit: 10, EnableInterleaving: true, SelectProperties: ["RefinableString10", "CreatedBy","Created"], RefinementFilters: ["RefinableString18:equals('false')"], }).then((result: SearchResults) => { var props = result.PrimarySearchResults; debugger; var propValue = ""; var counter = 1; props.forEach(function(object) { propValue += counter++ +'. Title - ' +object.RefinableString10 +"<br/>"+"Rank - " + object.Rank +"<br/>"+"File Type - " + object.FileType+"<br/>"+ "Original Path - " +object.OriginalPath +"<br/>"+" Summary - "+ object.HitHighlightedSummary + "<br/>"+"<br/>"; }); document.getElementById("spSearchresults").innerHTML = propValue; }).catch(function(err) { console.log("Error: " + err); }); } public render(): React.ReactElement<IReactsearchProps> { return ( <div className={styles.reactsearch}> <div className={styles.container}> <div className={`ms-Grid-row ms-bgColor-themeDark ms-fontColor-white ${styles.row}`}> <div className="ms-Grid-col ms-lg10 ms-xl8 ms-xlPush2 ms-lgPush1"> <span className="ms-font-xl ms-fontColor-white">Welcome to SharePoint!</span> <p className="ms-font-l ms-fontColor-white">Customize SharePoint experiences using Web Parts.</p> <p className="ms-font-l ms-fontColor-white">{escape(this.props.description)}</p> <a href="https://aka.ms/spfx" className={styles.button}> <span className={styles.label}>Learn more</span> </a> </div> </div> </div> </div> ); } }
This is the .tsx file. I also did the same import in the webpart.ts file.
- Maggan WåhlinOct 11, 2017Iron ContributorMaybe it is a package dependency issue? Check your versions in package.json. This is my config:
{
"name": "order-webpart",
"version": "0.0.1",
"private": true,
"engines": {
"node": ">=0.10.0"
},
"dependencies": {
"@microsoft/sp-core-library": "~1.1.0",
"@microsoft/sp-webpart-base": "~1.1.1",
"@types/react": "0.14.46",
"@types/react-addons-shallow-compare": "0.14.17",
"@types/react-addons-test-utils": "0.14.15",
"@types/react-addons-update": "0.14.14",
"@types/react-dom": "0.14.18",
"@types/webpack-env": ">=1.12.1 <1.14.0",
"jquery": "^2.2.4",
"react": "15.4.2",
"react-dom": "15.4.2",
"sp-pnp-js": "^3.0.1"
},
"devDependencies": {
"@microsoft/sp-build-web": "~1.1.0",
"@microsoft/sp-module-interfaces": "~1.1.0",
"@microsoft/sp-webpart-workbench": "~1.1.0",
"gulp": "~3.9.1",
"@types/chai": ">=3.4.34 <3.6.0",
"@types/mocha": ">=2.2.33 <2.6.0"
},
"scripts": {
"build": "gulp bundle",
"clean": "gulp clean",
"test": "gulp test"
}
}- Mike JansenOct 11, 2017Iron ContributorHmm, I do not have a "package.json" in my project. Should that be under the "Config" folder?.
I do have a "package-solution.json" but I'm not allowed to create any "dependencies" there.