Partilhar via


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