Sharepoint:检查用户是否是组的成员
如何检查用户(不是当前登录的用户)是否是某个组的成员? 尝试从不属于其成员的组中检索用户会导致 SPException,因此不可能检查 null。
那么你会如何解决这个问题。 目前,我考虑在 SPGroup.Users.XML 字符串中搜索用户名或迭代所有组成员并检查登录名。
更新: 我忘了提及,我想避免使用异常处理来检查用户的成员资格。
how can I check if a user (not the one currently logged in) is member of a certain group? Trying to retrieve a user from a group of which he's not a member leads to an SPException, so checking for null is not possible.
So how would you solve this problem. At the moment I think about searching in the SPGroup.Users.XML string for the user's name or iterating over all the group members and checking the login names.
Update:
I forgot to mention that I want to avoid the usage of exception handling to check the user's membership.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
为 SPUser 和静态方法创建扩展类< /a>:
然后在 SPUser 对象上调用此方法:
Create an Extension class for SPUser and static method:
Then invoke this method on your SPUser object:
我通过使用 LINQ 编写扩展方法来完成此操作。 SPGroup 继承自 SPPrincipal,因此您应该能够将其传递给
principal
参数:I have done this by writing an extension method using LINQ. SPGroup inherits from SPPrincipal so you should be able to pass it through to the
principal
parameter:有几种方法。 SharePoint 组有一个选项,可以仅允许组所有者查看成员资格详细信息或允许每个人查看成员资格详细信息。 如果允许每一项,您将不会受到安全限制,否则您需要 RunWithElevatedPrivileges,并确保获得 SPSite 和 SPSite 的新实例。 里面要用到SPWeb。
据说以下是选项:
另一种方法是
Couple of ways. SharePoint group has an Option that can allow only the Group Owner to see the membership details or allow everyone to view the membership details. If every one is allowed you will not get the Security restriction, else you need to RunWithElevatedPrivileges, and be sure to get a New Instance of the SPSite & SPWeb to be used inside that.
Being said that below are the options:
Another way is to
您是否尝试过使用RunWithElevatedPrivileges?
Have you tried using RunWithElevatedPrivileges?
为了使 SPSecurity.RunWith.. 工作,您需要有一个 SPSite 和/或 SPWeb 对象的新实例,并且不使用上下文,否则它将无法工作。
您有 SPUser 对象可以使用吗? 如果是这样,您可以只使用 SPUser.Groups 集合。
In order to make the SPSecurity.RunWith.. work, you need to have a new instance of the SPSite and/or SPWeb object and not use the context otherwise it won't work.
Do you have the SPUser object to use? If so, you can just use the SPUser.Groups collection.
我实现了一种简单的方法来检查特定 SharePoint 组中是否存在特定用户。 SPUser 对象上使用 linq 的简单语句。
查找有关 SharePoint 核心解决方案。
I have implemented a simple way to check if specific user exists in a specific SharePoint group. A simple statement with linq on SPUser object.
Find the detailed post on SharePoint Core Solutions.