DotNet Mirror
  DNM facebook   DNM Google+   DNM Twitter   

Programmatically remove SharePoint group from the site

By Ashok Nalam on 21 May 2013 | Category: Sharepoint | Tagged: groups .NET 
snippet covers how to remove SharePoint group from site.
  Discuss Add Comment   |  
Add rating Rate this resource   
Using the below code we can remove SharePoint group from the site. RemoveSPGroup is like generic method, which accepts parameters as SPSite object, and Group name then deletes the group from site.

code:

//Removes Group from site
public void RemoveSPGroup(SPSite site,string groupName)
{
	using (SPWeb oWebsiteRoot = site.RootWeb)
	{
		if (CheckGroupExistsInSiteCollection(oWebsiteRoot, groupName))
		{
			SPGroupCollection siteGroups = oWebsiteRoot.SiteGroups;
			siteGroups.Remove(groupName);
		}
	}
}
//checks group exists in site collection
private static bool CheckGroupExistsInSiteCollection(SPWeb web, string groupName)
{
  return web.SiteGroups.OfType<SPGroup>().Count(g => g.Name.Equals(groupName, StringComparison.InvariantCultureIgnoreCase)) > 0;
}

Method call:

SPSite currentSite = SPContext.Current.Site;
RemoveSPGroup(currentSite,"Group Visitors")

Output:

Removes "Group Visitors" from site
  Discuss Add Comment    
Add rating Rate this resource   
About the Contributor
Member Since : 10 Dec 2012
Member Points (Level) : 7003  (Expert)
Location : INDIA
Home Page : http://dotnetmirror.com
About : I am admin of this site.
Rate this resource
 
Add your Comment
Name Email WebSite
Captcha Refresh


Comments (0)
No comments found, click here to add comment.