Export Sharepoint List Items In Excel (SPFX React)🚀

Introduction

 
react-csv is a powerful plugin written in React.js to export an array of items to Excel files. In this article i am using PnpV2 library for consuming Sharepoint operations.
 
Open a command prompt. Create a directory for the SPFx solution.
 
md spfx-ReactCSV
 
Navigate to the above-created directory.
 
cd spfx-ReactCSV
 
Run the Yeoman SharePoint Generator to create the solution.
 
yo @microsoft/sharepoint
 
Solution Name
 
Hit Enter to have the default name (spfx-ReactCSVin this case) or type in any other name for your solution.
Selected choice - Hit Enter
 
Target for the component
 
Here, we can select the target environment where we are planning to deploy the client web part; i.e., SharePoint Online or SharePoint OnPremise (SharePoint 2016 onwards).
Selected choice - SharePoint Online only (latest).
 
Place of files
 
We may choose to use the same folder or create a subfolder for our solution.
Selected choice - same folder.
 
Deployment option
 
Selecting Y will allow the app to be deployed instantly to all sites and be accessible everywhere.
Selected choice - N (install on each site explicitly).
 
Permissions to access web APIs
 
Choose if the components in the solution require permission to access web APIs that are unique and not shared with other components in the tenant.
Selected choice - N (solution contains unique permissions)
 
Type of client-side component to create
 
We can choose to create a client-side web part or an extension. Choose the web part option.
Selected choice - WebPart
 
Web part name
 
Hit Enter to select the default name or type in any other name.
Selected choice - ReactCsv
 
Web part description
 
Hit Enter to select the default description or type in any other value.
 
Framework to use
 
Select any JavaScript framework to develop the component. Available choices are - No JavaScript Framework, React, and Knockout.
Selected choice - React
 
The Yeoman generator will perform a scaffolding process to generate the solution. The scaffolding process will take a significant amount of time.
 
Once the scaffolding process is completed, lock down the version of project dependencies by running the below command,
 
npm shrinkwrap
 
In the command prompt, type the below command to open the solution in the code editor of your choice.
 
NPM Packages used,
  1. npm i react-csv  //for Csv operations
  1. npm install @pnp/[email protected] --save  //for sharepoint operations
  1. npm install @pnp/polyfill-ie11 --save  //for iell support
Important setup
  1. protected onInit(): Promise<void> {  
  2.   
  3.   return super.onInit().then(_ => {  
  4.   
  5.     // other init code may be present  
  6.   
  7.    sp.setup({  
  8.   // set ie 11 mode  
  9.   ie11: true,  
  10.   // only needed when working within SharePoint Framework  
  11.   spfxContext: this.context  
  12. });  
  13.   });  
  14. }  
Necessary imports
  1. import "@pnp/polyfill-ie11";  
  2. import { sp } from "@pnp/sp";  
  3. import "@pnp/sp/webs";  
  4. import "@pnp/sp/lists";  
  5. import "@pnp/sp/items";  
  6. import { CSVLink, CSVDownload } from "react-csv";  
plugin Intialisation
  1. <CSVLink data={csvData}>Download me</CSVLink>;  
Full Code
 
in ReactCsv.tsx
  1. import * as React from 'react';  
  2. import { IReactCsvProps } from './IReactCsvProps';  
  3. import { sp } from "@pnp/sp";  
  4. import "@pnp/sp/webs";  
  5. import "@pnp/sp/lists";  
  6. import "@pnp/sp/items";  
  7. import { CSVLink, CSVDownload } from "react-csv";  
  8. export interface MYListProperties {    
  9.   Title: string;  
  10.   Platform :String;  
  11.     
  12. }    
  13. interface IPnpstate {      
  14.   MyListData:MYListProperties[];    
  15. }   
  16. export default class ReactCsv extends React.Component<IReactCsvProps, IPnpstate> {  
  17.   constructor(props: IReactCsvProps, state: IPnpstate) {    
  18.     super(props);    
  19.     this.state = {    
  20.       MyListData: []   
  21.     };  
  22.     this.MYdata=this.MYdata.bind(this);  
  23.   }  
  24.     public componentDidMount(){    
  25.       this.MYdata().then((response: MYListProperties[]) => {    
  26.         let result: MYListProperties[] = [];  
  27.         response.forEach(element => {  
  28.         result.push({  
  29.        Title: element.Title, Platform: element.Platform  
  30.         });  
  31.         });   
  32.         this.setState({ MyListData: result });    
  33.           
  34.       });    
  35.     }  
  36.   private  MYdata=async ()=>{  
  37.     const items: MYListProperties[] = await sp.web.lists.getByTitle("ReactCSV").items.get();  
  38.     return items;  
  39.     console.log(items);  
  40.   
  41.   }  
  42.   public render(): React.ReactElement<IReactCsvProps> {  
  43.  this.MYdata();  
  44.     return (  
  45.       <div >  
  46.        { this.state.MyListData.length>0 &&  <CSVLink data={this.state.MyListData}  filename={"my-file.csv"}  
  47.   className="btn btn-primary"  onClick={() => {  
  48.     console.log("You Downloaded the Csv"); // 👍🏻 Your click handling logic  
  49.   }}>Download me</CSVLink> }  
  50.       
  51.       </div>  
  52.     );  
  53.   }  
  54. }  
Here I am using list name as ReactCsv and fields as Title,Platform both are single lines of text.
 
Properties
  • data- array of data
  • filename-name of the csv file to download
  • className-class file for css properties
  • onClick-clickEvent
  • headers-head value of the cell 
Expected output
 
Export Sharepoint List Items in Excel(SPFX React)
 
Export Sharepoint List Items in Excel(SPFX React)
 

Conclusion

 
Hence we learned how to export Sharepoint list items to csv files using pnpjs and reactcsv. I hope this helps someone. Happy coding :)