SPFx + PnP: how do I get web part property values inside the .then() ?

Iron Contributor

My web part needs to:

  1. check to see if the list it depends on exists and if so
  2. query the list for items and
  3. display the items returned, using properties of the web part to modify the display

Should be simple.  When I put a break point on the first line of code inside the .then(), I can see that the list items are being returned, but I have no way to access the wp property values because "this" is now undefined.

Test for list existenceTest for list existenceGet list items to display, but properties are unavailableGet list items to display, but properties are unavailable

How do I access the values of the web part properties when I am inside a .then() call so I can use those values to modify how the items are displayed?

 

I am guessing this is something simple, but I am having trouble figuring how to do this.  Thanks.

3 Replies

*Bump*

 

Out sick for a week and not a single response? I'm honestly a bit surprised.  I know that "globals" are much unloved (and with good reason), but if I'm building a web part, it's properties should be available to me everywhere in my project. They don't do me any good if I can't access them.  The fact that the properties we define and populate are not available to the rest of the code in the project (i.e. like a public global object) feels like an oversight in the design of the SharePoint Framework.

 

Perfectly willing to eat crow due to my ignorance, but I really need some guidance here.

 

As described above, once the data has been retrieved using PnP, I need format it for display based on properties values set in the property pane by the end user. I cannot do that if the values contained in those properties cannot be accessed.  Someone please tell me what I am missing here.

 

Thanks.

  1. Are you using react?
  2. Is this in a .ts file or a .tsx component?
  3. How are you setting the questionClass property and what are you using it for?

As a first step, assuming you're working at the root of your web part (webPartNameWebPart.ts) your properties will be undefined by default when you load the web part, you should set default values for your property pane properties in the manifest.json for the web part see an example of manifest.json below:

{
  "$schema": "../../../node_modules/@microsoft/sp-module-interfaces/lib/manifestSchemas/jsonSchemas/clientSideComponentManifestSchema.json",
  "id": "You'reID",
  "alias": "HelloWorldWebPart",
  "componentType": "WebPart",
  "version": "*",
  "manifestVersion": 2,
  "requiresCustomScript": false,
  "preconfiguredEntries": [
    {
      "groupId": "You'reGroupID",
      "group": {
        "default": "Under Development"
      },
      "title": {
        "default": "hellow World"
      },
      "description": {
        "default": "This is the defualt description"
      },
      "officeFabricIconFontName": "Page",
//Below is where you set the property defaults, you need to have one for each of the properties you have setup. If you have an array that isn't defined here (even though it's blank) it will be undefined at runtime
      "properties": {
        "descripton": "description text",
        "booleanProperty": true,
        "arrayProperty": [],
        "numberProperty": 0,
      }
    }
  ]
}

There is some more about preconfiguredEntries here: http://sharepointseb.co.uk/2017/08/17/updating-preconfiguredentries/

@Sebastian Burrell,

 

Thanks for the response. I learned about this late last week from a MS guy over at the dev docs site: https://github.com/SharePoint/sp-dev-docs/issues/759 

 

I've basically taken them to task for this silly kludge; if you define the properties in the property pane configuration (after all, the name implies you are configuring the properties!), I feel strongly that you shouldn't have to do it again in the manifest.  Why have an option in there to set a default value if it will just be ignored?  We shouldn't have to define the same thing twice.

 

As to your questions:

1. No, no react framework in this project

2. It's in the plain ol' <webpartname>.ts

3. User's putting the web part on the page can use "questionClass" to add class names that they want to use to alter the way the data is displayed.  If they've added one or more class names, I want to add those to the rendering at runtime.

 

I like the tip in your link as well.  Thanks again.