top of page
Writer's pictureNakkeeran Natarajan

Retrieve Content Types From SharePoint Site Using PnP JavaScript Library

Introduction

In this article, you will learn how to perform the basic content type read operations on SharePoint sites, using PnP JavaScript Core Library.


Note

The operations, given below, get information from the site collection or site or sub sites. The logic can be executed by adding the script references on the content editor Web part or any normal custom Web part from the sites. The results will be retrieved only from the respective site, where the code is executed.


The PnP JavaScript core library is supported by SharePoint 2013, SharePoint 2016 On Premises and Office 365 versions. The following operations are tested on SharePoint 2013 and Office 365 environments:


Prerequisite

The required JavaScript core library references for executing any operations, using PnP, are: 

  • es6-promise.js

  • fetch.js

  • pnp.js or pnp.min.js

The script references should be called from the Web parts, only in the order given above.


Note: You can download the references from my initial article about PnP JavaScript Core library. 

I explained about the necessity of using the promise and retrieving JS references in the article mentioned above. You can also download these references from Github site, as well.


We can set up the header before executing any operation. This is required, when you want the response type to be predetermined. Let us see, how we can get the JSON data as a response. This has to be done once, before initiating all the requests on the page. This step is not mandatory for O365 environment.


The code snippet, given below, shows setting up the header:

  1. $pnp.setup({      

  2. headers: {  

  3. "Accept": "application/json; odata=verbose",      

  4. },  

  5. });  


Retrieve Site Content Types

In this section, you will learn, how to retrieve all the site content types available on a site, using PnP JavaScript Core Library.


First, we need to retrieve the content type collection from the Web object. Get method is used to retrieve all the content type information.


The following code snippet helps in retrieving all the content types and properties.

  1. $pnp.sp.web.contentTypes.get().then(function(data)

  2. {      

  3. console.log("There are totally " + data.length + " content types available on the site");  

  4. for(var i=0; i < data.length; i++){          

  5. console.log(data[i].StringId + " - " + data[i].Name);             

  6. }         

  7. }).

  8. catch(function(data){      

  9. console.log(data);  

  10. });  


Retrieve Site Content Types with Selected Properties

In this section, you will learn, how to retrieve all the site content types from a site with only selected content type properties, using PnP JavaScript Core Library. Select method filters the required properties.


The following code snippet shows all the content types for a site collection with only view title property filtered, using the select method.

  1. $pnp.sp.web.contentTypes.select('StringId','Name','Description').get().then(function(data)

  2. {      

  3. console.log("There are totally " + data.length + " content Types available on the site");  

  4. for(var i=0; i < data.length; i++)

  5. {          

  6. console.log("Content Type Name : " + data[i].Name);          

  7. console.log("Content Type ID : " + data[i].StringId);          

  8. console.log("Content Type Description : " + data[i].Description);         

  9.  console.log("----------------------------------------------");      

  10. }     

  11. }).

  12. catch(function(data){      

  13. console.log(data);  

  14. });  


Retrieve Required Content Type

In this section, you will learn, how to retrieve only the required content type from the site using content type Id.


The following code snippet retrieves a content type with the properties by passing the content type ID, using getById method.

  1. $pnp.sp.web.contentTypes.getById('0x01').get().then(function(data)

  2. {     

  3.  console.log("Content Type Name : " + data. Name);      

  4. console.log("Content Type ID : " + data.StringId);      

  5. console.log("Content Type Description : " + data.Description);     

  6.  console.log("Content Type XML : " + data.SchemaXml);  

  7. }).

  8. catch(function(data)

  9. {      

  10. console.log(data); 

  11.  });  

The following snapshot shows the result on the console.


Retrieve Required Content Type with Select Properties

In this section, you will learn, how to retrieve only the required content type, using content type ID with only the required properties, using getById and select methods.


The following code snippet retrieves a content type from a site by using content type ID and gets only the name of the content type in the response. Other properties are undefined in the response.

  1. $pnp.sp.web.contentTypes.getById('0x01').select('Name').get().then(function(data)

  2. {          

  3. console.log(data.Name);  

  4. }).

  5. catch(function(data){      

  6. console.log(data);  

  7. });  


Note: The results can be viewed on the Browser's debugger console for the operations, given above.


Summary

Thus, you have learned, how to retrieve the content types and their properties from a site in different ways, using PnP JavaScript core library. PnP JavaScript library is supported by SharePoint 2013, SharePoint 2016 On Premises and Office 365 versions. The operations, mentioned above, are tested in SharePoint 2013 and Office 365 environments.

0 comments

Comments


bottom of page