Check for Group existence in SharePoint Site

In SharePoint 2013 server side object model (SSOM), it appears that the Method overloads of the SPGroupCollection such as GetByName() or indexer all throw an exception when the group is not found.  However, you can still use reflection to get non throwing version of the methods. In my project I ended up creating an extension method that wraps the reflection based function call.

public static class SPGroupCollectionExtensions
{
  public static SPGroup GetByNameNoThrow(this SPGroupCollection group, string groupName)
  {
    var method = group.GetType().GetMethod("GetByNameNoThrow", BindingFlags.Instance | BindingFlags.NonPublic);
var parameters = new object[] {groupName};
    return (SPGroup)method.Invoke(group, parameters);
   }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s