Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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.

thanks.

share|improve this question
    
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. –  coder1 Dec 4 '12 at 17:37
add comment

2 Answers 2

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.

HTH

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.