Retrieve SP List Item's Column Value In SPFx Application Customizer Using PNP

In this blog article, I am going to describe all the steps required to get SharePoint list field values by using SharePoint Framework model which is SPFx.

If you are already familiar with SPFx then you might have a better understanding on SPFx application customizer.

If you are new to SPFx then please go through this below-mentioned link from where you can get a better understanding on SPFx model.

https://docs.microsoft.com/en-us/sharepoint/dev/spfx/sharepoint-framework-overview

In this below-mentioned code example, I have taken application customizer of SPFx. I have also used PnP JavaScript library. The Patterns and Practices JavaScript Core Library was created to help developers by simplifying common operations within SharePoint and the SharePoint Framework.

To get more detailed information about PnP javascript library, please follow this link,
 
https://github.com/SharePoint/PnP-JS-Core.

This below-mentioned code example is very simple and straight forward. At first, you have to import ‘sp-pnp-js’ namespace, then you can use PnP library in code to retrieve SharePoint list and its field values.

 

This code snippet will help you to retrieve any column values of all list items present under a particular list. You just have to provide your list title and list field/column internal name in the following line,

pnp.sp.web.lists.getByTitle(“My List”).items.select(“Title”, “Id”, “CustomField_1”, “CustomField_2”).getPaged()

In the above piece of code “My list” is SP list title and “Title”, “Id”, “CustomField_1”, “CustomField_2” are list field/column internal names.

Accordingly you can change these values on your end,
  1. import { override } from '@microsoft/decorators';  
  2. import { Log } from '@microsoft/sp-core-library';  
  3. import {  
  4. BaseApplicationCustomizer  
  5. }  
  6. from '@microsoft/sp-application-base';  
  7. import {  
  8.     Dialog  
  9. } from '@microsoft/sp-dialog';  
  10. require('mutationobserver-shim');  
  11. import * as strings from 'HelloWorldApplicationCustomizerStrings';  
  12. const LOG_SOURCE: string = 'HelloWorldApplicationCustomizer';  
  13. /** 
  14.  * If your command set uses the ClientSideComponentProperties JSON input, 
  15.  * it will be deserialized into the BaseExtension.properties object. 
  16.  * You can define an interface to describe it. 
  17.  */  
  18. export interface IHelloWorldApplicationCustomizerProperties {  
  19.     // This is an example; replace with your own property  
  20.     testMessage: string;  
  21. }  
  22. let _observer: MutationObserver;  
  23. /** A Custom Action which can be run during execution of a Client Side Application */  
  24. export default class HelloWorldApplicationCustomizer  
  25. extends BaseApplicationCustomizer < IHelloWorldApplicationCustomizerProperties > {  
  26.     @override  
  27.     public onInit(): Promise < void > {  
  28.         Log.info(LOG_SOURCE, `Initialized ${strings.Title}`);  
  29.         let message: string = this.properties.testMessage;  
  30.         if (!message) {  
  31.             message = '(No properties were provided.)';  
  32.         }  
  33.         this.context.placeholderProvider.changedEvent.add(thisthis.renderObserver);  
  34.         return Promise.resolve();  
  35.     }  
  36.     private async renderObserver(): Promise < void > {  
  37.         if (!_observer) {  
  38.             // Register observable  
  39.             let config = {  
  40.                 childList: true,  
  41.                 subtree: true  
  42.             };  
  43.             _observer = new MutationObserver(this.addMyCustomMenu);  
  44.             _observer.observe(document, config);  
  45.         }  
  46.     }  
  47.     private addMyCustomMenu(mutationsList) {  
  48.         var o365Menus = document.querySelectorAll("*[class^=\"o365cs-mfp-linklist o365cs-segoeRegular o365cs-text-align-left\"]");  
  49.         if (o365Menus.length > 0) {  
  50.             for (var i in o365Menus) {  
  51.                 try {  
  52.                     var menuItem = o365Menus[i];  
  53.                     var innerhtml = menuItem.innerHTML;  
  54.                     var newhtml = '<div class="ms-fcl-tp o365cs-nfd-normal-lineheight"><a class="ms-fcl-tp o365button" role="link"';  
  55.                     newhtml += 'href="javascript:alert(\"Hello\");" id="O365_SubLink_CustomMenu"';  
  56.                     newhtml += 'aria-labelledby="_ariaId_9"><span class="_fc_3 owaimg" style="display: none;"></span>';  
  57.                     newhtml += '<span class="_fc_4 o365buttonLabel" id="_ariaId_9">My Custom Menu</span></a></div>';  
  58.                     menuItem.innerHTML = innerhtml.concat(newhtml);  
  59.                 } catch (e) {}  
  60.             }  
  61.         }  
  62.     }  
  63.     private _onDispose(): void {  
  64.         if (_observer) {  
  65.             _observer.disconnect();  
  66.         }  
  67.         console.log('[HelloWorldApplicationCustomizer._onDispose] Disposed custom.');  
  68.     }  
  69. }