【问题标题】:How to tell if a Principal Id is a permission group or a single user?如何判断 Principal Id 是权限组还是单个用户?
【发布时间】:2021-08-15 01:26:44
【问题描述】:

我正在使用 pnpJS 列出对 SPO 项目具有权限的所有用户:

 var info = await sp.web.lists.getByTitle('MyList').items.getById(theId2).roleAssignments();
    console.log(info, 'info');
var info2 = await sp.web.lists.getByTitle('MyList').roleAssignments.getById(3).groups();
    console.log(info2, 'groups');
    var newArr = info.map(a => a.PrincipalId);
    console.log(newArr, 'newArr');
    const res = newArr.forEach(async el => {
       await sp.web.siteUsers.getById(el).get().then(u => {
        console.log(u.Title);
      });
    });

我正在获取用户列表,但如果有一个组分配给 SPO 项目,则会引发错误。我需要推断出什么是组,什么是用户,这样我就可以阻止 foreach 包含组,从而消除错误。

对于 newArr 日志,我得到:

我知道 Principal Id 3 是一个组。我希望 foreach 忽略它返回的任何组。成功地做到这一点将停止我得到的错误。 我意识到 roleAssigments 可能正在寻找用户而不是组,这会导致错误。但是我将如何列出与该项目关联的任何组?

更新(其他人的工作代码):

const userIds = [];
    const userNames = [];
    const res = newArr.forEach(async el => {
      // console.log(el, 'el');
      try {
        await sp.web.siteUsers.getById(el).get().then(u => {
           console.log(u.Title);
           userNames.push(u.Title);
           userIds.push(el);
         });
       } catch (err) {
         console.error("looks like it wasn't user after all");
       }

     });
   this.setState({
     userIds: userIds,
     userNames: userNames
    },() => {
      console.log(this.state.userIds, 'userIds');
      console.log(this.state.userNames, 'userNames');
    });
 console.log("userIds: ", userIds); // see if you get your "good" ids here
 // vs
 console.log("user and group ids:", newArr);

【问题讨论】:

  • infonewArr 的日志打印什么?是否有一些属性可以区分您正在使用的数据中的组和用户?
  • 我会根据您的问题更新我的问题。

标签: reactjs sharepoint sharepoint-online pnp-js


【解决方案1】:

查看info 数据,它似乎没有提供任何区别。

处理这些数据的潜在方法是添加 try / catch 块并继续使用所需的任何功能,并在循环时获取实际用户列表。

// 确实感觉很老套,但看看是否有帮助

 var info = await sp.web.lists.getByTitle('MyList').items.getById(theId2).roleAssignments();
    var newArr = info.map(a => a.PrincipalId);
    const userIds = []; // store collection of user ids that are not groups
    newArr.forEach(async el => {
      try {
       await sp.web.siteUsers.getById(el).get().then(u => {
          console.log(u.Title);
          userIds.push(el); // OR store actual user you get in response
        });
      } catch (err) {
        console.error("looks like it wasn't user after all", err);
      }
    });

console.log("userIds: ", userIds); // see if you get your "good" ids here
// vs
console.log("user and group ids:", newArr);

【讨论】:

  • 我会试试这个并报告!谢谢@Uma
  • 不,它不起作用。我在最后一行的“void”类型上不存在属性“过滤器”,并且在“any []”类型上也不存在属性“包含”。您需要更改目标库吗?尝试将 lib 编译器选项更改为 'es2016' 或更高版本。ts(2550) 用于最后一行的 'includes'
  • 我已经用一些可能有用的东西更新了我的问题。
  • 您实际上可能不需要过滤结果(事实上,我建议过滤 ForEach 的结果永远不会奏效,因为它只是遍历数组而不创建新数组)。让我更新答案
  • 感谢您的帮助 @Uma 我会用答案更新我的问题
猜你喜欢
  • 2011-10-28
  • 1970-01-01
  • 1970-01-01
  • 2012-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多