kamagra how much to take

Test Whether SPUser Is In SPGroup

I had to write a small method this morning that would allow me to test whether an SPUser object was a member of an arbitrary SPGroup. Particularly, I needed a way to do this in an event receiver, which is why you see a reference to the properties parameter representative of the SPItemEventProperties when getting a reference to the relevant item and list objects. I wanted to do this basically:

  1. SPUser curUser = web.CurrentUser;
  3. SPGroup testGroup = properties.ListItem.Web.Groups["Home Visitors"];
  4. string groupName = testGroup.Name;
  5. if (bIsInGroup(curUser, groupName))
  6. {
  9. }

Then to use this boolean test, I simply just used this method:

  1. private bool bIsInGroup(SPUser user, string strname)
  2. {
  3. try
  4. {
  5. foreach (SPGroup group in user.Groups)
  6. {
  7. if (group.Name == strname)
  8. return true;
  9. }
  10. }
  11. catch (Exception exception)
  12. {
  13. WriteLogEvent(string.Format("An Error Occured | Exception Message:{0} StackTrace: {1}", exception.Message, exception.StackTrace));
  14. }
  15. return false;
  16. }

And just using the traditional way of writing errors to the event log:

  1. public void WriteLogEvent(string strmessage)
  2. {
  3. // Create a new event comLog object
  4. EventLog comLog = new EventLog("Application");
  6. // Check whether the event comLog source already exists
  7. if (!EventLog.SourceExists("test"))
  8. EventLog.CreateEventSource("test", "Application");
  10. // Specify the source for the exception message
  11. comLog.Source = "test";
  12. // Write the entry to the event comLog
  13. comLog.WriteEntry(strmessage);
  14. }


  1. Chris White says:

    Hi Adam,

    I’ve looked into this too at the following article: http://chrissyblanco.blogspot.com/2007/07/determining-whether-user-is-member-of.html

    I looked at doing it your way i.e iterating the Groups collection of the SPUser object, however this didn’t appear to work for the following scenario:

    Say you have a user who is a member of an AD group e.g. ADGroupX. You then add ADGroupX to a SharePoint group (SPGroup), say SharePointGroupY.

    If you iterate the Groups collection of the SPUser object for the user who is a member of ADGroupX then SharePointGroupY is not one of the SPUser.Groups . . .

    Have you seen this issue too ?!

    Many thanks,


  2. Rob says:

    Just had to say thanks. This is a concise solution to a problem I was fighting against.

  3. santosh says:

    i want to check the selected user’s group how would i??(i have a list of users in a dropdown)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>