I have different users in various sharepoint groups(Admin,User,Approver) in my site collection.Now in my visual webpart how can I validate users(from these above groups) who are going to login.

Help required.


  • Validate them for what purpose? They're validated if they can log in. Do you want to know if a user is part of a certain group? – David Lozzi Dec 4 '12 at 17:30
  • Yes lets say someone from approver group logs in,he should not able to see some labels,grids etc. – user13408 Dec 4 '12 at 17:37

You can explore the SPGroup object. Grab the group from the web.SiteGroups["Group Name"], and then call method group.ExplicitlyContainsCurrentUser(), which will check if the current user is in the group.

This won't work if you're using AD groups however. Instead, you'll need to get group.Users and iterate through, finding the AD groups (that appear as SPUser objects, yes, really), then query AD to find out if the user is in the group. There's a few ways to do that, search online for loads of examples.



You can use the IsCurrentUserMemberOfGroup() method on the SPWeb object:

int groupID = SPContext.Current.Web.Groups["Group Name"].ID;
bool isGroupMember = SPContext.Current.Web.IsCurrentUserMemberOfGroup(groupID);

Although, since according to your comment above, the reason you want this information is to hide controls form certain users, you may be able to use the SPSecurityTrimmedControl to hide the controls based on permission levels rather than group membership.

And... after a quick search to find some informative blog posts that might explain how to use the Security Trimmed Control, I actually found one that gives instructions on how to extend it to support groups.

So there you go.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy