PnP-JS-Core: Check if group exists in SharePoint site

This post will show you how to check the group exists in SharePoint site 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

This post explains,

How to check the SharePoint group exists on site based on group name using PnP JavaScript Library.

Example:

The below steps and code snippet used to check the SharePoint exists on current site 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">

    $pnp.sp.web.siteGroups.filter("Title eq ‘Group Name’").get().then(function(result) {
    if (result.length > 0) {
    document.getElementById("sample").innerHTML = "Group Exist!";
    } else {
    document.getElementById("sample").innerHTML = "Group doesn’t exist!!!";
    }
    });

    </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, “Group Exist!” – if the group available in site. if group name not found, the page displays “Group doesn’t exist!!!“.
Shantha Kumar
Shantha Kumar
Articles: 278

24,849 Comments

  1. This is very informative blog. I learnt a lot and planning to implement in my next project.

    Thank you for sharing.

Comments are closed.