Retrieve And Display SharePoint List Items Using REST API And ReactJS

React makes it easy to create interactive UIs. It helps us to create simple views for each state in our application, efficiently update and render the right components when our data changes. In this section, we will make use of React and REST api to retrieve list items from SharePoint and display them using Content Editor web part.

Retrieve SharePoint List data using REST API and display using Content Editor Webpart

Elements are the smallest building blocks of React apps.It describes what we want to see on the UI.

  1. const element = <h1>Display me at root node</h1>;   

reactdom.render is the starting point of the React component. Let's say we have a <div> somewhere in our HTML file.

  1. <div id="root"></div>   

So, to render our React element into the above root DOM node, we will pass both to ReactDOM.render().

  1. const element = < h1 > Display me at root node < /h1>;  
  2. ReactDOM.render(element, document.getElementById('root'));  

This will display the message at root div. In our case, we will be displaying the data retrieved from 'ProductSales' list in the div named 'CarSalesData'.

  1. ReactDOM.render(<ReactSP />, document.getElementById('CarSalesData'));   

ReactSP represents the component that will be rendered at the CarSalesData div. ReactSP which is the component name is defined as plain javascript class which extends React.Component abstract class.We will also define at least one render method within it and will be defining the UI within this render method.

Within the class, we will also have a constructor which is the right place to initialize state. We will update this state with SharePoint data at a later point in the react lifecycle. If we don't have to initialize state and we are not binding any methods, we don't need to implement a constructor for our React component.

Each component has several "lifecycle methods" that we can override to run code at a particular time of the process. Methods that are prefixed with 'will' are called just before some event happens, and methods prefixed with 'did' are called just after an event happens. We will be making use of the 'componentDidMount' method to fetch data from SharePoint List and we will update the state with this data. In the render method, we will then read the state data and display it in the UI.

SharePoint

REACT and REST API script to display SharePoint data as Grid

The code can be saved as a text file and added to the Content Editor WebPart to display the Grid WebPart.

  1. <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.5.4/react.js"></script>    
  2. <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.5.4/react-dom.min.js"></script>    
  3. <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.24.0/babel.min.js"></script>    
  4. <div id="CarSalesData"></div>  
  5. <script type="text/babel">   
  6.    var tableStyle  =    {    
  7.           display: "table",  
  8.           marginLeft : "40px"  
  9.       }  
  10.    var panelStyle  =    {    
  11.          background: "#91A4A7"  
  12.       }  
  13.      
  14.    var divStyle = {  
  15.     background: "#eee",  
  16.     padding: "20px",  
  17.     margin: "20px"  
  18.    };  
  19.      
  20.    var headerCaptionStyle = {  
  21.     background: "#4B6978",  
  22.      display: "table-cell",   
  23.           border: "solid",  
  24.           textAlign : "center",  
  25.           width : "500px",  
  26.           height : "30px",  
  27.           paddingTop : "3px",  
  28.           color : "white",  
  29.           marginLeft : "80px",  
  30.           display : "block"  
  31.    };  
  32.      
  33.    var headerStyle = {  
  34.     background: "#4B6978",  
  35.      display: "table-cell",   
  36.           border: "solid",  
  37.           textAlign : "center",  
  38.           width : "100px",  
  39.           height : "30px",  
  40.           paddingTop : "10px",  
  41.           color : "white"  
  42.    };  
  43.      
  44.    var tableCaptionStyle = {  
  45.    background: "#c6e2ff",  
  46.      display: "block",  
  47.      fontSize : "20px",  
  48.      fontWeight: "bold",   
  49.           border: "solid",  
  50.           textAlign : "center",  
  51.           width : "650px",  
  52.           height : "30px",  
  53.           paddingTop : "3px",  
  54.           borderRadius: "25px",  
  55.           marginLeft : "30px",  
  56.           marginTop : "20px"  
  57.    }  
  58.      
  59.      
  60.    var rowCaptionStyle = {  
  61.     width : "600px",  
  62.     display :  "table-caption",  
  63.     background: "#4B6978",  
  64.     textAlign : "center",  
  65.     padding: "20px",  
  66.      fontSize : "20px",  
  67.     fontWeight :"bold",  
  68.     color : "white"  
  69.    };  
  70.      
  71.    var rowStyle = {  
  72.     display :  "table-row",  
  73.     background: "#eee",  
  74.     padding: "20px",  
  75.     margin: "20px",  
  76.     fontWeight :"bold"  
  77.    };  
  78.      
  79.    var CellStyle  =    {    
  80.           display: "table-cell",   
  81.           border: "solid",  
  82.            borderColor : "white",  
  83.           textAlign : "center",  
  84.           width : "100px",  
  85.           height : "30px",  
  86.           paddingTop : "10px"  
  87.             
  88.       }    
  89.      
  90.       class ReactSP extends React.Component{    
  91.          debugger;  
  92.           constructor(){    
  93.               super();    
  94.                 this.state = {    
  95.         items: [    
  96.           {    
  97.             "CarName""",    
  98.             "Quarter1""",    
  99.             "Quarter2":"",    
  100.             "Quarter3""",  
  101.             "Quarter4":""    
  102.           }    
  103.         ]    
  104.       };    
  105.      
  106.      }    
  107.              
  108.           componentDidMount() {    
  109.               debugger;  
  110.              this.RetrieveSPData();    
  111.           }    
  112.       
  113.           RetrieveSPData(){    
  114.               var reactHandler = this;    
  115.       
  116.               var spRequest = new XMLHttpRequest();    
  117.               spRequest.open('GET'"/sites/playground/_api/web/lists/getbytitle('ProductSales')/items",true);    
  118.               spRequest.setRequestHeader("Accept","application/json");  
  119.                                   
  120.               spRequest.onreadystatechange = function(){    
  121.                     
  122.                   if (spRequest.readyState === 4 && spRequest.status === 200){    
  123.                       var result = JSON.parse(spRequest.responseText);    
  124.                           
  125.                       reactHandler.setState({    
  126.                           items: result.value  
  127.                       });    
  128.                   }    
  129.                   else if (spRequest.readyState === 4 && spRequest.status !== 200){    
  130.                       console.log('Error Occured !');    
  131.                   }    
  132.               };    
  133.               spRequest.send();    
  134.           }    
  135.       
  136.           render(){   
  137.             debugger;  
  138.              return (    
  139.         <div style={panelStyle}>   
  140.           <br></br>  
  141.      
  142.           <br></br>   
  143.           <div style={tableCaptionStyle} > Demo : Retrieve SharePoint List Items using React JS  </div>  
  144.           <br></br>  
  145.             
  146.           <div style={tableStyle} >     
  147.              <div style={rowCaptionStyle} > Quarterly Car Sales Data </div>  
  148.             <div style={rowStyle} >    
  149.               <div style={headerStyle}>Car Name</div>    
  150.               <div style={headerStyle}>Quarter 1 </div>    
  151.               <div style={headerStyle}>Quarter 2</div>    
  152.                 <div style={headerStyle}>Quarter 3</div>    
  153.                <div style={headerStyle}>Quarter 4</div>         
  154.             </div>    
  155.               
  156.               {this.state.items.map(function(item,key){    
  157.                   
  158.                 return (<div style={rowStyle} key={key}>    
  159.                     <div style={CellStyle}>{item.CarName}</div>    
  160.                     <div style={CellStyle}>{item.Quarter1}</div>    
  161.                      <div style={CellStyle}>{item.Quarter2}</div>  
  162.          <div style={CellStyle}>{item.Quarter3}</div>  
  163.          <div style={CellStyle}>{item.Quarter4}</div>  
  164.                   </div>);    
  165.               })}    
  166.                       
  167.           </div>    
  168.         </div>    
  169.             
  170.       );    
  171.     }    
  172.               
  173.           
  174.       }    
  175.       
  176.       ReactDOM.render(<ReactSP />, document.getElementById('CarSalesData'));    
  177. </script>   

Summary

Thus, we saw how to retrieve and display SharePoint List Items using React JS and REST API.