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;
  2.  
  3. SPGroup testGroup = properties.ListItem.Web.Groups["Home Visitors"];
  4. string groupName = testGroup.Name;
  5. if (bIsInGroup(curUser, groupName))
  6. {
  7.  
  8.  
  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");
  5.  
  6. // Check whether the event comLog source already exists
  7. if (!EventLog.SourceExists("test"))
  8. EventLog.CreateEventSource("test", "Application");
  9.  
  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. }
Share

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>