Retrieve Web Content Types From SharePoint Using Pnp JavaScript Library

PnP-JS-Core library contains the number of extensible methods and properties using which we can achieve 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 collections which are used to form a SharePoint site. Those are used in managing the content and generating reports based on contents and settings etc.

In this post, we will use the PnP-JS-Core method to get the collection of content types from the SharePoint Site.

Example

The following code snippet is used to return the collection of Content Type and its properties from the current SharePoint site, using PnP JavaScript library. Click here to read more about getting the SharePoint Content Types.

  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.       
  5. <div id="sample"></div>  
  6.     
  7. <script type="text/javascript">  
  8. //contentTypes property returns all content types from the site  
  9. //then( ) runs on success  
  10. //catch( ) runs on failure  
  11.    
  12. $pnp.sp.web.contentTypes.get().then(function(result) {  
  13.     console.log(result);  
  14.     var contetTypeInfo = "";  
  15.     for (var i = 0; i < result.length; i++) {  
  16.         contetTypeInfo += "Name: " + result[i].Name + "<br/>ID:" + result[i].StringId + "<br/><br/>";  
  17.     }  
  18.     document.getElementById("sample").innerHTML = contetTypeInfo;  
  19. }).catch(function(err) {  
  20.     alert(err);  
  21. });  
  22. </script>