In the following code, we will retrieve the value of the default columns. For example - “Title”, “Id” etc. and also the value of some custom columns.
In the following code block, I have retrieved the following column values.
- Title
- Id
- MyCustomFld which is a custom column
- MyLookUpFld which is a custom column(Lookup column).
Before you start using the code attached, you have to first install pnp using the below command.
npm install sp-pnp-js –save
The code block for this is mentioned below.
- import {
- override
- } from '@microsoft/decorators';
- import {
- Log
- } from '@microsoft/sp-core-library';
- import {
- BaseApplicationCustomizer
- } from '@microsoft/sp-application-base';
- import {
- Dialog
- } from '@microsoft/sp-dialog';
- import pnp from 'sp-pnp-js';
- import * as strings from 'HelloWorldApplicationCustomizerStrings';
- const LOG_SOURCE: string = 'HelloWorldApplicationCustomizer';
-
-
-
-
-
- export interface IHelloWorldApplicationCustomizerProperties {
-
- testMessage: string;
- }
-
- export default class HelloWorldApplicationCustomizer
- extends BaseApplicationCustomizer < IHelloWorldApplicationCustomizerProperties > {
- @override
- public onInit(): Promise < void > {
- pnp.setup({
- spfxContext: this.context
- });
-
- pnp.sp.web.lists.getByTitle("My List").items.select("Title", "Id", "MyCustomFld", "MyLookUpFld/ID").expand("MyLookUpFld").getPaged().then(p => {
- console.log(JSON.stringify(p.results, null, 4));
- var itemColl = p.results;
- for (var index = 0; index < itemColl.length; index++) {
- var element = itemColl[index];
- var title = element["Title"];
- var id = element["Id"];
- var customFldValue = element["MyCustomFld"];
- var lookUpFld = element["MyLookUpFld"];
- var lookUpFldId = lookUpFld["ID"];
- console.log("Item with Id: " + id + " and title: " + title + " has MyCustomFld field value: " + customFldValue + " and MyLookUpFld lookup field value: " + lookUpFldId);
- }
- });
- return Promise.resolve();
- }
- }
After PnP is installed successfully, use the attached code in your SPFX solution to retrieve the SPList column values.