Retrieve SP list item’s column value in SPFx application customizer using PNP

  • Posted by: Softree Technology

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.

pnp

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:

[code lang=”c”]
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’;
require(‘mutationobserver-shim’);

import * as strings from ‘HelloWorldApplicationCustomizerStrings’;

const LOG_SOURCE: string = ‘HelloWorldApplicationCustomizer’;

/**
* If your command set uses the ClientSideComponentProperties JSON input,
* it will be deserialized into the BaseExtension.properties object.
* You can define an interface to describe it.
*/
export interface IHelloWorldApplicationCustomizerProperties {
// This is an example; replace with your own property
testMessage: string;
}
let _observer: MutationObserver;
/** A Custom Action which can be run during execution of a Client Side Application */
export default class HelloWorldApplicationCustomizer
extends BaseApplicationCustomizer<IHelloWorldApplicationCustomizerProperties> {

@override
public onInit(): Promise<void> {
Log.info(LOG_SOURCE, `Initialized ${strings.Title}`);

let message: string = this.properties.testMessage;
if (!message) {
message = ‘(No properties were provided.)’;
}

this.context.placeholderProvider.changedEvent.add(this, this.renderObserver);

return Promise.resolve();
}

private async renderObserver(): Promise<void> {

if (!_observer) {
// Register observable
let config = { childList: true, subtree: true };
_observer = new MutationObserver(this.addMyCustomMenu);
_observer.observe(document, config);
}
}

private addMyCustomMenu(mutationsList) {
var o365Menus = document.querySelectorAll(“*[class^=\”o365cs-mfp-linklist o365cs-segoeRegular o365cs-text-align-left\”]”);
if (o365Menus.length > 0) {
for (var i in o365Menus) {
try {

var menuItem = o365Menus[i];
var innerhtml = menuItem.innerHTML;
var newhtml = ‘<div class=”ms-fcl-tp o365cs-nfd-normal-lineheight”><a class=”ms-fcl-tp o365button” role=”link”‘;
newhtml += ‘href=”javascript:alert(\”Hello\”);” id=”O365_SubLink_CustomMenu”‘;
newhtml += ‘aria-labelledby=”_ariaId_9″ style=””><span class=”_fc_3 owaimg” style=”display: none;”></span>’;
newhtml += ‘<span class=”_fc_4 o365buttonLabel” id=”_ariaId_9″>My Custom Menu</span></a></div>’;

menuItem.innerHTML = innerhtml.concat(newhtml);
} catch (e) { }
}
}
}

private _onDispose(): void {
if (_observer) {
_observer.disconnect();
}
console.log(‘[HelloWorldApplicationCustomizer._onDispose] Disposed custom.’);
}
}

[/code]

This solution is brought to you by our SharePoint professionals.

Softree Consulting employs SharePoint consultants; we are a technology services provider with the aim to help companies achieve exceptional performance through SharePoint. Our dedicated team of SharePoint consultants has the right bent of mind to understand and execute customer requirements.

Be it SPFx or SharePoint add-in developments, SharePoint 2019 developments, web part developments, migrating from SharePoint 2010/2013 to SharePoint 2013/2016/Office 365, Office 365, SharePoint hosted apps development or something else in SharePoint, we strive to deliver the best

Author: Softree Technology

Leave a Reply