top of page
Writer's pictureNakkeeran Natarajan

Retrieve Sub Sites From Sharepoint Sites Or Sub Sites Using Pnp Javascript Core Library

Introduction

In this article, you will learn how to retrieve all the sub sites from SharePoint sites or sub sites, using PnP JavaScript Core Library.


Note:

The operations, given below, get the sub sites from the SharePoint sites (can be site collection or sub site as well). The logic can be executed by adding the script references on the content editor web part or any normal custom web part from site collections, or sites, or sub sites. The results will be retrieved only from the respective site where the code is executed, because we are using web object for data retrieval.


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 above order.


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

I have explained about the necessity of using the references, in the above article. You can also download these references from GithubĀ site, as well.


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.


Access all sub sites of SharePoint Site or Sub Site

Here, we will see how we can fetch all the sub sites that are available on the current site, using PnP JavaScript library.


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 Sub Sites with Properties

We will see how we can access the available sub sites from the current site, using PnP JavaScript code.

The code snippet, given below, lists down all the sub sites available on the current site. It shows the sub sites count for a current site. This will also show the sub sites' title and URL.

  1. $pnp.sp.web.webs.get().then(function(data){Ā Ā Ā Ā Ā Ā 

  2. console.log("ThereĀ areĀ totallyĀ "Ā +Ā data.lengthĀ +Ā "Ā subsitesĀ availableĀ onĀ theĀ siteĀ (SCOPEĀ :Ā WEB)");Ā Ā 

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

  4. console.log("SubĀ SiteĀ TitleĀ :Ā "Ā +Ā data[i].Title);Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 

  5. console.log("SubĀ SiteĀ URLĀ Ā Ā :Ā "Ā +Ā data[i].Url);Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 

  6. console.log("---------------------------------");Ā Ā Ā Ā Ā Ā 

  7. }Ā Ā 

  8. })

  9. .catch(function(data){Ā Ā }

  10. ); Ā 


Retrieve Sub Sites with only required properties

The required property values can only be retrieved using the select method. In the select method, the required property names are passed as string array.


As many property names as you want, can be passed to get the values.


The code snippet, given below, gets only the required property values. It shows the sub sites count for a site where the script is configured. This will also show the sub site title and the URL.

  1. $pnp.sp.web.webs. select("Title","Url","Description").get().then(function(data){Ā Ā Ā Ā Ā Ā 

  2. console.log("ThereĀ areĀ totallyĀ "Ā +Ā data.lengthĀ +Ā "Ā subsitesĀ availableĀ onĀ theĀ siteĀ (SCOPEĀ :Ā WEB)");Ā Ā 

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

  4. console.log("SubĀ SiteĀ TitleĀ :Ā "Ā +Ā data[i].Title);Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 

  5. console.log("SubĀ SiteĀ URLĀ Ā Ā :Ā "Ā +Ā data[i].Url);Ā Ā Ā Ā Ā Ā Ā Ā Ā 

  6. Ā console.log("DescriptionĀ Ā Ā Ā :Ā "Ā +Ā data[i].Description);Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 

  7. console.log("---------------------------------");Ā Ā Ā Ā Ā Ā 

  8. }Ā Ā 

  9. }).

  10. catch(function(data){Ā Ā }

  11. );Ā Ā 


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


Summary

Thus, you have learned how to retrieve the sub sites of a site (or sub site) and their properties, 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