我正在使用 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);