How to Check if SPGroup Exists
The SharePoint API lacks methods to check for the security group existence. Here is utilities that do the job:
public static bool GroupExists(SPGroupCollection groups, string name)
{
if (string.IsNullOrEmpty(name) ||
(name.Length > 255) ||
(groups == null) ||
(groups.Count == 0))
return false;
else
return (groups.GetCollection(new String[] { name }).Count > 0);
}
public static bool GroupExists(SPGroupCollection groups, int id)
{
if ((id < 0) ||
(groups == null) ||
(groups.Count == 0))
return false;
else
return (groups.GetCollection(new Int32[] { id }).Count > 0);
}
Comments
Anonymous
June 15, 2009
PingBack from http://stevepietrek.com/2009/06/15/links-6152009/Anonymous
January 04, 2011
Just to make you aware that your code is being plagiarized here: aarohblah.blogspot.com/.../how-to-check-if-spgroup-exist.html Thanks for the original post.Anonymous
March 01, 2011
Thanks for the code, pretty cool because checking if the group is null doesn't work.Anonymous
June 05, 2011
The comment has been removedAnonymous
June 13, 2011
I get the same error... anyone assist please?Anonymous
June 22, 2011
I get the same error - has anyone found a fix for the Operation is not valid due to the current state of the object." errorAnonymous
May 21, 2012
Here a possible solution to this problem zsvipullo.blogspot.it/.../verificare-che-un-spgroup-esista.htmlAnonymous
April 02, 2013
Thanks for the help! Much appreciated.