C# code to find SharePoint web application Group's.
- public List<NameValuePair> GetGroups(string siteUrl)
- {
- List<NameValuePair> groups = new List<NameValuePair>();
- using (SPSite site = new SPSite(siteUrl))
- {
- using (SPWeb web = site.OpenWeb())
- {
- foreach (SPGroup group in web.Groups)
- {
- groups.Add(new NameValuePair() { Value = group.Name, Name = group.Name });
- }
- }
- }
- return groups;
- }
- public List<NameValuePair> GetGroupsWithOwner(string siteUrl)
- {
- List<NameValuePair> groups = new List<NameValuePair>();
- using (SPSite site = new SPSite(siteUrl))
- {
- using (SPWeb web = site.OpenWeb())
- {
- string ownerName = string.Empty;
- foreach (SPGroup group in web.Groups)
- {
- if (group.Owner is SPUser)
- {
- ownerName = (group.Owner as SPUser).Name;
- }
- else if (group.Owner is SPGroup)
- {
- ownerName = (group.Owner as SPGroup).Name;
- }
- groups.Add(new NameValuePair() { Value = group.Name, Name = string.Format("{0} ({1})", group.Name, ownerName) });
- }
- }
- }
- return groups;
- }
- public List<NameValuePair> RemoveGroups(List<string> groups, string siteUrl)
- {
- List<NameValuePair> groupstatus = new List<NameValuePair>();
- using (SPSite site = new SPSite(siteUrl))
- {
- using (SPWeb web = site.OpenWeb())
- {
- NameValuePair groupstat;
- foreach (string group in groups)
- {
- groupstat = new NameValuePair();
- try
- {
- web.Groups.Remove(group);
- groupstat.Name = string.Format("{0} removed sucessfully.", group);
- groupstat.Value = true;
- }
- catch (Exception ex)
- {
- groupstat.Name = string.Format("Error {1} while removing gorup {0}", group, ex.Message);
- groupstat.Value = false;
- }
- groupstatus.Add(groupstat);
- }
- }
- }
- return groupstatus;
- }

Join the conversation! Your thoughts help the community grow.