Safeguard your crypto investments with Trezor App's robust security infrastructure, protecting them from potential risks.

PnP-JS-Core: Get Site Collection Context Information

This post shows the syntax and code example to get the site collection context information using PnP JavaScript Library

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

Simplified JavaScript Library for SharePoint

PnP-JS-Core Library

In this post explains,

How to get the Site Collection context information using PnP JavaScript Library.

Syntax:

pnp.sp.site.getContextInfo  – Gets the context information for the site collection.

Example:

The below steps and code snippet used to get the Site Collection’s context information using PnP JavaScript library,

  1. Download Required files to use PnP-JS-Core library from the below links and upload that to Site Asstes or Style Library
    • Download pnp.js  PnP JS file
    • Download fetch.js Used by PnP js file to handle web requests and responses (Required in IE)
    • Download promise.js Used by PnP js file to handle web requests and responses (Required in IE)
  2. Create new web part page and insert Content Editor web part
  3. Create a sample.html file in Site Assets or Style library and insert the below code snippet

    [code language=”javascript”]

    <script type="text/javascript" src="/siteassets/scripts/fetch.js"></script>
    <script type="text/javascript" src="/siteassets/scripts/promise.min.js"></script>
    <script type="text/javascript" src="/siteassets/scripts/pnp.min.js"></script>

    <div id="sample"></div>

    <script type="text/javascript">
    //The below PnP property used to return the basic information about the site collection
    $pnp.sp.site.getContextInfo().then(function(result) {

    var contextInfo = "";
    for (var key in result) {
    contextInfo += key + " : " + result[key] + "<br/>";
    }

    document.getElementById("sample").innerHTML = contextInfo;
    //console.log(contextInfo);
    });
    </script>

    [/code]

  4. Add the URL of sample.html file to the content editor web part
  5. Click ok to apply the changes to the web part and save the page.
  6. Now the page displays all context information returned by the PnP JavaScript method .

Output:

Site Context Information using PnP-JS-Core
Site Context Information using PnP-JS-Core
Shantha Kumar
Shantha Kumar
Articles: 278