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 Lists
  • Get List by Title
  • Create List
  • Delete 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.       
  8.     context.load(lists);  
  9.     context.executeQueryAsync(function () {    
  10.       var listenum = lists.getEnumerator();    
  11.       while (listenum.moveNext()) {    
  12.           var listItem = listenum.get_current();    
  13.             
  14.           siteContent += `<div>  
  15.                           <span>${listItem.get_title()}</span>  
  16.                         </div>`;    
  17.       }    
  18.         
  19.       htmlContext.domElement.querySelector("#siteContent").innerHTML =siteContent;      
  20.     },  
  21.     function(sender,args){  
  22.       console.log(args.get_message());  
  23.     });  
  24.   }   

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.       
  9.     context.load(list);  
  10.     context.executeQueryAsync(function () {    
  11.       siteContent += `<div>  
  12.                           <div><h2>Title   : ${list.get_title()}</h2></div>  
  13.                           <div>Description : ${list.get_description()}</div>  
  14.                           <div>Template    : ${list.get_baseTemplate()}</div>  
  15.                           <div>Total Items : ${list.get_itemCount()}</div>  
  16.                         </div>`;   
  17.         
  18.       htmlContext.domElement.querySelector("#siteContent").innerHTML =siteContent;      
  19.     },  
  20.     function(sender,args){  
  21.       console.log(args.get_message());  
  22.     });  
  23.   }   

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.       
  8.     var listCreationInfo = new SP.ListCreationInformation();  
  9.     listCreationInfo.set_title('TestList1');  
  10.     listCreationInfo.set_templateType(SP.ListTemplateType.genericList);  
  11.   
  12.     const list:SP.List = lists.add(listCreationInfo);  
  13.   
  14.       
  15.     context.load(list);  
  16.     context.executeQueryAsync(function () {    
  17.       siteContent += `<div>  
  18.                           <div><h2>Title   : ${list.get_title()}</h2></div>  
  19.                           <div>Description : ${list.get_description()}</div>  
  20.                           <div>Template    : ${list.get_baseTemplate()}</div>  
  21.                           <div>Total Items : ${list.get_itemCount()}</div>  
  22.                         </div>`;   
  23.         
  24.       htmlContext.domElement.querySelector("#siteContent").innerHTML =siteContent;      
  25.     },  
  26.     function(sender,args){  
  27.       console.log(args.get_message());  
  28.     });  
  29.   }    

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();  
  6.     const lists: SP.ListCollection = web.get_lists();  
  7.     const list:SP.List = lists.getByTitle("TestList1");  
  8.   
  9.     list.deleteObject();  
  10.     context.executeQueryAsync(function () {    
  11.       siteContent += `<div>TestList1 list deleted</div>`;   
  12.         
  13.       htmlContext.domElement.querySelector("#siteContent").innerHTML =siteContent;      
  14.     },  
  15.     function(sender,args){  
  16.       console.log(args.get_message());  
  17.     });  
  18.   }    

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.

Up Next
    Ebook Download
    View all
    Learn
    View all