Basic Web Operations Using PnP JS Core In SharePoint

Introduction

In this blog, we will cover how to use the PnP JS Core library to do some basic Website operations in SharePoint. To know more about this library, check the links, given below:

Basic Web Operations

PnP JS Core library is developed from SharePoint REST API to simplify the development process and ease the developers burden in creating SharePoint client based Applications. It supports all type of operations, which are supported by SharePoint REST API.

The basic Web operations like creating a Website, reading a Website, updating a Website and deleting a Website are achievable in a minimum number of code than other SharePoint libraries.

To use this library in the code, we have to refer to PnP JS file in the project or the script. The latest PnP JavaScript file is available from the dist folder under PnP-JS-Core Github location.

We can download and upload this file to SharePoint location and include in our Application or we can also directly use this location in our project as a CDN.

Creating website:

The following examples creates a new child site under the current context site.

  1. <script type=”text/javascript” src=”/siteasstes/scripts/pnp.min.js”></script>  
  2. <script type=”text/javascript”>  
  3.     //The following line creates a new child site to the current site with the url as “newsite” and title as “New Sub Site”  
  4.     $pnp.sp.web.webs.add("New Sub Site", "newsite", "Description for the new Site", "STS", "1033", true).then(function(result) {  
  5.         if (result.data.Created) {  
  6.             alert('Site created successfully');  
  7.         }  
  8.     });  
  9. </script>  
Reading website

The following examples return the current Website information.
  1. <script type=”text/javascript” src=”/siteasstes/scripts/pnp.min.js”></script>  
  2. <script type=”text/javascript”>  
  3.     //The following line gets the web object and shows the web properties (title & description)  
  4.     $pnp.sp.web.get().then(function(web) {  
  5.         alert("Title: " + web.Title + "\r\n" + "Description: " + web.Description);  
  6.     });  
  7. </script>  
Updating Website

The following example updates the description to the current context Website and in success, gets and shows the value of the current Website description.
  1. <script type=”text/javascript” src=”/siteassets/scripts/pnp.min.js”></script>  
  2. <script type=”text/javascript”>  
  3.     //The following line updates the description property to the current web  
  4.     $pnp.sp.web.update({  
  5.         Description: 'A sample description'  
  6.     }).then(function(success) {  
  7.         success.web.get().then(function(result) {  
  8.             console.log(result.Description);  
  9.         });  
  10.     });  
  11. </script>  
Deleting website

The following example deletes the current Website.
  1. <script type=”text/javascript” src=”/siteassets/scripts/pnp.min.js”></script>  
  2. <script type=”text/javascript”>  
  3.     //The following line deletes the current website  
  4.     $pnp.sp.web.delete().then(function(result) {  
  5.         alert(‘Site deleted successfully.’);  
  6.     });  
  7. </script>  
Place the Script in CEWP or Script editor and also you can try in the Browser console.

To work the code, given above in Internet Explorer, we have to include the fetch.js and es6-promise.js JavaScript files as a reference in addition to pnp.js file.

Conclusion

In this article, we have learned the basic Web CRUD operations, using PnP JavaScript library. Click here to read the same article from my blog. 
Ebook Download
View all
Learn
View all