SharePoint List Operations Using SPFx Solution With JSOM


Introduction

Here, let us look at performing basic list operations, using SharePoint JavaScript Object Model (JSOM) on SharePoint Framework Solutions (SPFx).


Microsoft provides the packages for supporting JSOM operations. Usually in the traditional approach, JavaScript files required for JSOM operations are MicrosoftAjax.js, SP.js, SP.runtime.js etc. For SPFx solutions, the required files can be included by installing the required typings.


In my previous article, you will have learned building the code, using JSOM approach on SharePoint Framework Solutions.


The support files are installed, using the typings. Install the required typings (Microsoft.ajax and Sharepoint) before creating the project.


Create the project with no JavaScript framework option, using the command yo @microsoft/sharepoint. In Render method, insert the necessary HTML wrapper to display the data.


Declare other methods to process the data. Four methods are declared to retrieve SharePoint data. 

Get ListsGet List by TitleCreate ListDelete List 


Get lists

Let us look at retrieving all the lists from SharePoint site, using JSOM approach.


The context of the object is set and then the list collection will be retrieved, using the Web object. The list collection is loaded and executed with the context before looking at the data. Now, using Get Enumerator method, individual list properties are retrieved and displayed.

  1. public GetLists(){  

  2. var htmlContext = this;      

  3. let siteContent:string = "";  

  4. const context: SP.ClientContext = new SP.ClientContext(this.context.pageContext.web.absoluteUrl);  

  5. const web: SP.Web = context.get_web();  

  6. const lists: SP.ListCollection = web.get_lists();       

  7. context.load(lists);      

  8. context.executeQueryAsync(function () 

  9. {    

  10. var listenum = lists.getEnumerator();    

  11. while (listenum.moveNext()) {    

  12. var listItem = listenum.get_current();               

  13. siteContent += `<div>                            

  14. <span>${listItem.get_title()}

  15. </span>                          

  16. </div>`;          

  17. }           

  18. htmlContext.domElement.querySelector("#siteContent").innerHTML =siteContent;          

  19. },  

  20. function(sender,args){        

  21. console.log(args.get_message());      

  22. });    

  23. }  


Get list by Title

Let us see retrieving the required list, using the title with JSOM approach.


Like the above method, the context is used to retrieve the list collection, using the Web object with context URL. From the list collection, the required list is extracted with getbytitle() method. Now, the list is loaded and executed, using the context. In the success method, the properties are displayed.

  1. public GetList(){  

  2. var htmlContext = this;      

  3. let siteContent:string = "";  

  4. const context: SP.ClientContext = new SP.ClientContext(this.context.pageContext.web.absoluteUrl);  

  5. const web: SP.Web = context.get_web();  

  6. const lists: SP.ListCollection = web.get_lists();  

  7. const list: SP.List = lists.getByTitle("TestList");       

  8. context.load(list);      context.executeQueryAsync(function () 

  9. {          

  10. siteContent += `<div>                            

  11. <div><h2>Title   : ${list.get_title()}</h2>

  12. </div>                            

  13. <div>Description : ${list.get_description()}

  14. </div>                            

  15. <div>Template    : ${list.get_baseTemplate()}

  16. </div>                            

  17. <div>Total Items : ${list.get_itemCount()}</div>                          

  18. </div>`;          

  19. htmlContext.domElement.querySelector("#siteContent").innerHTML =siteContent;          

  20. },  

  21. function(sender,args){        

  22. console.log(args.get_message());      

  23. });    

  24. }  


Create list

Let us create a list, using JSOM approach. Like the previous method, the list collection is retrieved. To the list collection, a new list is added using the list creation information object. The list creation object contains the basic list information. Now, the list is loaded and executed , using the context.

  1. public CreateList(){  

  2. var htmlContext = this;      

  3. let siteContent:string = "";  

  4. const context: SP.ClientContext = new SP.ClientContext(this.context.pageContext.web.absoluteUrl);  

  5. const web: SP.Web = context.get_web();  

  6. const lists: SP.ListCollection = web.get_lists();  

  7. var listCreationInfo = new SP.ListCreationInformation();      

  8. listCreationInfo.set_title('TestList1');      

  9. listCreationInfo.set_templateType(SP.ListTemplateType.genericList);  

  10. const list:SP.List = lists.add(listCreationInfo);       

  11. context.load(list);      

  12. context.executeQueryAsync(function () {          

  13. siteContent += `<div>                            

  14. <div><h2>Title   : ${list.get_title()}</h2>

  15. </div>                            

  16. <div>Description : ${list.get_description()}

  17. </div>                            

  18. <div>Template    : ${list.get_baseTemplate()}

  19. </div>                            

  20. <div>Total Items : ${list.get_itemCount()}</div>                         

  21.  </div>`;          

  22. htmlContext.domElement.querySelector("#siteContent").innerHTML =siteContent;          

  23. },  

  24. function(sender,args){        

  25. console.log(args.get_message());      

  26. });    

  27. }   


Delete list

Here, we will see deleting the existing list from the site by JSOM approach. The list is retrieved like Get list by Title method. Now, the list object is deleted, using deleteObject() method. Now, the context is executed.

  1. public DeleteList(){  

  2. var htmlContext = this;      

  3. let siteContent:string = "";  

  4. const context: SP.ClientContext = new SP.ClientContext(this.context.pageContext.web.absoluteUrl);  

  5. const web: SP.Web = context.get_web();   const lists: SP.ListCollection = web.get_lists();  

  6. const list:SP.List = lists.getByTitle("TestList1");       

  7. list.deleteObject();      

  8. context.executeQueryAsync(function () 

  9. {          

  10. siteContent += `<div>TestList1 list deleted</div>`;          

  11. htmlContext.domElement.querySelector("#siteContent").innerHTML =siteContent;          

  12. },  

  13. function(sender,args){        

  14. console.log(args.get_message());      

  15. });    

  16. }   


Deploy/ Run

To test the solution, using the command prompt, run Gulp serve command for testing. From the required site, open the workbench.aspx file and add the Web part. The Web part will display the site properties.

To deploy the solution, the solution should be bundled and packaged. Now, the files should be uploaded to SharePoint site (Office 365 site) and then the app is deployed. 


The article given below shows the detailed steps for bundling, packaging and deploying. 


Summary

Thus, you have learned developing the list operations on SharePoint Framework Solutions with JavaScript Object Model (JSOM) approach.

Recent Posts

See All