Thursday, March 6, 2014

How to make sure if a particular SharePoint Group Exists or Not?

Inorder to know if a particular group exists in sharepoint group collection , just use the following function..

 private bool GroupExists(SPGroupCollection objgroups, string strName)
        {
            const string NAME = "Name";
            bool isGroupExists = false;
            if (string.IsNullOrEmpty(strName) ||

                (strName.Length > 255) ||

                (objgroups == null) ||

                (objgroups.Count == 0))
            {

                isGroupExists = false;
            }

            else
            {
                XDocument doc = XDocument.Parse(objgroups.Xml);
                isGroupExists = doc.Descendants().Where(g => g.Attribute(NAME) != null).Where(g => g.Attribute(NAME).Value.Equals(strName, StringComparison.InvariantCultureIgnoreCase)).Count() > 0;
            }
            return isGroupExists;

        }


The function would return true if the group exists in group collection of sharepoint..


You just have to call the function as shown below :



                SPGroupCollection objSiteGroups = YOURWEBOBJECT.SiteGroups;
                if (GroupExists(objSiteGroups, "YOUR GROUP NAME"))
                {
                   // Do all that you want with the group coz the group does exists
                }
Happy sharepointing :)


No comments:

Post a Comment

Rename a Web Application in Sharepoint : A solution using powershell

Here is a small script that will enable you all to change a SharePoint Web Application name . We can use the following SharePoint Pow...