Thursday, November 12, 2009

How to read current user's group name from sharpeoint site


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;
}




Thanks And Regards,
Dipti Chhatrapati



No comments:

Post a Comment