简体   繁体   中英

How can I check if current user is part of a sharepoint group with PnPJS?

I need to display content based on the group the current user belongs to but can't figure out how to do this in react/pnpjs.

I wrote the function below but it returns false even when the group name returned in console.log(grp["Title"]) is correct.

private _checkUserInGroup(strGroup)
  {

    let InGroup:boolean = false;

    let grp = sp.web.currentUser.groups.get().then((r: any) => {      
      r.forEach((grp: SiteGroups) =>{
        if (grp["Title"] == strGroup)
        {
           InGroup = true; 
        }
        console.log(grp["Title"]);
      });
    });

    return InGroup;
  }

Your request is pretty wide because we don't really know what you have tried.

Step wise

  1. Figure out what groups the current user is a member of.
  2. Fetch appropriate data in to some form of memory store.
  3. Display the data from memory store.

Which step is it that you are struggling with?

https://pnp.github.io/pnpjs/sp/site-users/

import { sp } from "@pnp/sp";
import "@pnp/sp/webs";
import "@pnp/sp/site-users/web";

let groups = await sp.web.currentUser.groups();

Cheers

Truez

I managed to make this work by placing async/await in the function...it was returning false before the call to sp.web.... Thanks a lot for the help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM