top of page

Developing SharePoint Web Parts With ReactJS - Part Two


In this article, you will learn how to develop SharePoint web parts using React JavaScript library and with the help of offline React compilers.

In my previous article, you have learned the basics, prerequisites, and the environment setup for developing SharePoint web parts using React offline compilers.

Steps Involved

Create/ Open project

Open the project using IDE - in this case, Visual Studio code. If you don't have any IDE, you can simply use notepad to create necessary files.

Create necessary files

HTML file to hold the html elements.

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

  2. <script src="">

  3. </script>  <script src="">

  4. </script>  <script type="text/javascript" src="../Style Library/scripts/hiworld.js">

  5. </script>  

Create folder to hold the React components (.jsx files).

Create a .jsx file to put the React data (sample.jsx).

  1. class GetUserDetails extends React.Component{      

  2. constructor(props) {  

  3. super(props);  

  4. this.state = {              

  5. user:""         

  6. };      

  7. }      

  8. componentWillMount(){  

  9. this.GetListItems();      

  10. }       

  11. GetListItems(){  

  12. var reactHandler = this;  

  13. var request = new XMLHttpRequest();          

  14.'GET', `/_api/Web/CurrentUser?

  15. $select=Title`, true);          

  16. request.setRequestHeader('Accept', 'application/json;odata=verbose;');          

  17. request.onreadystatechange = function()

  18. {  

  19. if (request.readyState === 4 && request.status === 200){  

  20. var returnedJson = JSON.parse(request.responseText);  

  21. if(returnedJson.d != null && returnedJson.d.Title != null)

  22. {                      

  23. reactHandler.setState(                          

  24. {                              

  25. user: returnedJson.d.Title                          

  26. }                      

  27. );                  

  28. }              

  29. }  

  30. else

  31. if (request.readyState === 4 && request.status !== 200)

  32. {                  

  33. console.log('Error in retrieving data!');              

  34. }          

  35. };          

  36. request.send();      

  37. }      

  38. render(){  

  39. return (              

  40. <div>                  

  41. Welcome {this.state.user}!              

  42. </div>          

  43. );       

  44. }  }  

  45. ReactDOM.render(    

  46. <GetUserDetails />,    document.getElementById('welcomespuser')  

  47. );  

Create a folder to hold JavaScript files. JavaScript files will not be created manually here. The files (generated) translated using the babel compiler will be present here.

From the root folder, create .babelrc file. This will help in compiling your JSX content into any virtual DOM elements. The file will contain the following code snippet.

  1. {  

  2. "plugins": ["transform-react-jsx"]  

  3. }  

The React component file will then, be translated to JavaScript file using babel compiler. Run the following command from the root folder to translate the .jsx content using the command prompt. 

  1. babel --preset react ReactFiles --watch --out-dir JSFiles  

The following snapshot shows the auto generated JavaScript file.

Note - The basic folder structure of the current project is shown below.

Deploy/ Run

Drop the HTML and JavaScript files into the SharePoint site (here, style library). Create any page with content editor web part or normal custom editor web part. Refer to the HTML and JavaScript files from the web parts, and then execute/run the page.

The below snapshot shows the content editor web part in edit mode which shows the HTML reference file placed on style library. The web part also displays the React component results (username of current logged in user). Similarly, the other data can be retrieved and shown.


Thus, you have learned developing the SharePoint web part using React components. The React file is compiled and translated to JavaScript file using the babel compiler. The translated JavaScript files are then used as references in HTML of SharePoint web part. 



bottom of page