Retrieve Visible Fields From SharePoint List Using PnP JavaScript Library


PnP-JS-Core library contains the number of extensible methods and properties, using which we can achieve the various actions in a simple code. To know more about this library component, visit the following links:

SharePoint has a lot of building blocks in the collections, which are used to form a SharePoint site. These are used in managing the content and generating the reports, based on the contents and settings etc.


We got a requirement to show the fields, available for the list item in a list or library.

  • To achieve the requirement, we are going to use the PnP JavaScript Library


To use the PnP JS library, we should add the script file given below to the references in our code.

  • Download pnp.js PnP JS file.
  • Download fetch.js used by PnP js file to handle the Web requests and responses (Required in IE).
  • Download es6-promise.js Used by PnP js file to handle web requests and responses (Required in IE).

Upload the above files to the library in SharePoint. Assume we have uploaded these files in Site Assets library for our example.

Code Skeleton

I have included all the above mentioned scripts and generated a skeleton code, given below, for adding the main code to retrieve the fields from the list:

  1. <script type="text/javascript" src="/siteassets/scripts/fetch.js"></script>  
  2. <script type="text/javascript" src="/siteassets/scripts/es6-promise.js"></script>  
  3. <script type="text/javascript" src="/siteassets/scripts/pnp.min.js"></script>  
  4. <div id="sample"></div>  
  5. <script type="text/javascript">  
  6.     //PnP Code snippet  
  7. </script>  
Replace the PnP Code snippet with the actual code.


The following code snippet is used to get the visible fields information from SharePoint list, using PnP JavaScript library. Click here to read more about getting the visible fields from the list.

In our example, we have a sample list (Telephones Growth), which contains the following custom columns.  
  • Year
  • Telephones (In Millions)
  • Wirelines 
  1. //fields property returns all fields from the retrieved list  
  2. //then( ) runs on success  
  3. //catch( ) runs on failure  
  4. $pnp.sp.web.lists.getByTitle("Telephones Growth").fields.filter("ReadOnlyField eq false and Hidden eq false").get().then(function(result)   
  5. {  
  6.     var fieldInfo = "";  
  7.     for (var i = 0; i < result.length; i++) {  
  8.         fieldInfo += "Title: " + result[i].Title + "<br/>";  
  9.         fieldInfo += "Name:" + result[i].InternalName + "<br/>";  
  10.         fieldInfo += "ID:" + result[i].Id + "<br/><br/>";  
  11.     }  
  12.     document.getElementById("sample").innerHTML = fieldInfo;  
  13. }).catch(function(err) {  
  14.     alert(err);  
  15. });  
Fields property from List object returns all the fields available for the specific SharePoint List. From all the fields, we have to apply the filter condition, given below, to retrieve the visible fields, available for the user to fill the details for the item.

Field ReadOnly == FALSE and Field Hidden == FALSE

Additional Notes

Supports SharePoint 2013, SharePoint 2016 and SharePoint Online.

For On-premise environment, PnP requests will not work properly since JSON Light is not enabled, by default. To enable, we have to modify the headers before calling PnP methods. Check PnP JSCore 1.0.1 for more information for setup / modifying the headers.

Insert the code snippet given below, before starting with PnP code:
  1. //Set response type in headers  
  2. $pnp.setup({  
  3.     headers: {  
  4.         "Accept""application/json; odata=verbose"  
  5.     }  
  6. });  
Complete Code

Create a sample .HTML file and add the complete code, given below. Add a content editor Web part to the page and add sample .HTML file URL to the Web part.
  1. <script type="text/javascript" src="/siteassets/scripts/fetch.js"></script>  
  2. <script type="text/javascript" src="/siteassets/scripts/es6-promise.js"></script>  
  3. <script type="text/javascript" src="/siteassets/scripts/pnp.min.js"></script>  
  4. <div id="sample"></div>  
  5. <script type="text/javascript">  
  6.     //Set response type in headers  
  7.     $pnp.setup({  
  8.         headers:   
  9.         {  
  10.             "Accept""application/json; odata=verbose"  
  11.         }  
  12.     });  
  13.     //fields property returns all fields from the retrieved list  
  14.     //then( ) runs on success  
  15.     //catch( ) runs on failure  
  16.     $pnp.sp.web.lists.getByTitle("TelePhones Growth").fields.filter("ReadOnlyField eq false and Hidden eq false").get().then(function(result)   
  17.     {  
  18.         var fieldInfo = "";  
  19.         for (var i = 0; i < result.length; i++)  
  20.         {  
  21.             fieldInfo += "Title: " + result[i].Title + "<br/>";  
  22.             fieldInfo += "Name:" + result[i].InternalName + "<br/>";  
  23.             fieldInfo += "ID:" + result[i].Id + "<br/><br/>";  
  24.         }  
  25.         document.getElementById("sample").innerHTML = fieldInfo;  
  26.     }).catch(function(err)  
  27.      {  
  28.         alert(err);  
  29.     });  
  30. </script>  

The code returns each field’s Title, Internal name and ID from SharePoint List. The output is shown below:



From this article, we learned to retrieve only the visible fields from SharePoint List / Library, using PnP JavaScript library. Click here to view the useful links for handling SharePoint with PnP JS Core Library.

Similar Articles