C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » C# Syntax »

Check if user belongs to given SPGroup or not in SharePoint


Posted Date: 04 May 2009    Resource Type: Code Snippets    Category: C# Syntax
Author: Dotnet GalaxyMember Level: Diamond    
Rating: 1 out of 5Points: 7



Hi All,

The following method to check if user belongs to given group in sharepoint. This is very useful utility function when we work on any sharepoint user management activities.


public bool IsUserAuthorized(string groupName)
{
//Retreiving the current context
SPSite site = SPContext.Current.Site;

//Opening a current web and creating a instance
using (SPWeb web = site.OpenWeb())
{
//Retreiving the current logged in user
SPUser currentUser = web.CurrentUser;

//Retrieving all the user groups in the site/web
SPGroupCollection userGroups = currentUser.Groups;

//Loops through the grops and check if the user is part of given group or not.
foreach (SPGroup group in userGroups)
{
//Checking the group
if (group.Name.Contains(groupName))
return true;
}
}
return false;
}


Hope it helps. Happy Coding...



Responses

Author: sara    02 Oct 2009Member Level: Bronze   Points : 1
Nice !!!!!!!!

get sharepoint user and group, it is simple.

Try this too,
Get Current SharePoint user


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Working with SPGroup  .  Method to check if user belongs to SPGroup or not  .  Enumerating user groups in SharePoint  .  Authorized user in SharePoint  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Get Page Header using WebHeaderCollection
Previous Resource: Export Data From Excel Sheet into Dataset
Return to Discussion Resource Index
Post New Resource
Category: C# Syntax


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use