Get, Add or Remove SharePoint groups and users programmatically

Below are some methods to get, add or remove SharePoint groups and group users.

Get site groups

public List<String> GetSiteGroups(string SiteURL)
{
	List<String> groups = new List<String>();
	SPSecurity.RunWithElevatedPrivileges(delegate()
	{
		using (SPSite site = new SPSite(SiteURL))
		{
			using (SPWeb web = site.OpenWeb())
			{
				SPGroupCollection sitegroups = web.SiteGroups;
				foreach (SPGroup item in sitegroups)
				{
					groups.Add(item.Name);
				}
			}
		}
	});

	return groups;
}

Ensure group

This method ensures that a group exists and if it doesn’t it creates a group with the specified name and owner (given as loginname)

public void EnsureSPGroup(string spGroup, string spGroupOwner, string SiteURL)
{
	SPSecurity.RunWithElevatedPrivileges(delegate()
	{
		using (SPSite site = new SPSite(SiteURL))
		{
			using (SPWeb web = site.OpenWeb())
			{
				try
				{
					SPGroup group = web.SiteGroups[spGroup];
				}
				catch (SPException ex)
				{
					SPUser owner = web.EnsureUser(spGroupOwner);
					web.SiteGroups.Add(spGroup, owner, null, string.Empty);
				}
			}
		}
	});
}

Get group members

This method returns all users members of a group specified with the group ID.

public List<String> getGroupUsers(int groupID, string SiteURL)
{
	List<String> groupMembers = new List<String>();
	SPSecurity.RunWithElevatedPrivileges(delegate()
	{
		using (SPSite site = new SPSite(SiteURL))
		{
			using (SPWeb web = site.OpenWeb())
			{
				SPGroup group = web.SiteGroups.GetByID(groupID);
				foreach (SPUser user in group.Users)
				{
					groupMembers.Add(user.LoginName);
				}
			}
		}
	});

	return groupMembers;
}

This method returns all users members of a group specified with the group name.

public List<String> getGroupUsers(string groupName, string SiteURL)
{
	List<String> groupMembers = new List<String>();
	SPSecurity.RunWithElevatedPrivileges(delegate()
	{
		using (SPSite site = new SPSite(SiteURL))
		{
			using (SPWeb web = site.OpenWeb())
			{
				SPGroup group = web.SiteGroups[groupName];
				foreach (SPUser user in group.Users)
				{
					groupMembers.Add(user.LoginName);
				}
			}
		}
	});

	return groupMembers;
}

Add user to a group

This method adds a user specified by login name to a group specified with the group ID

public void AddUser(String userLoginName, int groupID, string SiteURL)
{
	SPSecurity.RunWithElevatedPrivileges(delegate()
	{
		using (SPSite site = new SPSite(SiteURL))
		{
			using (SPWeb web = site.OpenWeb())
			{
				SPGroup group = web.SiteGroups.GetByID(groupID);
				SPUser userToAdd = web.EnsureUser(userLoginName);
				group.AddUser(userToAdd);
				group.Update();
			}
		}
	});
}

This method adds a user specified my login name to a group specified with the group name.

public void AddUser(String userLoginName, string groupName, string SiteURL)
{
	SPSecurity.RunWithElevatedPrivileges(delegate()
	{
		using (SPSite site = new SPSite(SiteURL))
		{
			using (SPWeb web = site.OpenWeb())
			{
				try
				{
					SPGroup group = web.SiteGroups[groupName];
					SPUser userToAdd = web.EnsureUser(userLoginName);
					group.AddUser(userToAdd);
					group.Update();
				}
				catch (Exception ex)
				{
					//handle the exception
				}
			}
		}
	});
}

Remove user from a group

This method removes a user from a group specified with the group ID.

public void RemoveUser(String userLoginName, int groupID, string SiteURL)
{
	SPSecurity.RunWithElevatedPrivileges(delegate()
	{
		using (SPSite site = new SPSite(SiteURL))
		{
			using (SPWeb web = site.OpenWeb())
			{
				SPGroup group = web.SiteGroups.GetByID(groupID);
				SPUser userToRemove = web.EnsureUser(userLoginName);
				group.RemoveUser(userToRemove);
				group.Update();
			}
		}
	});
}

This method removes a user from a group specified with the group name.

public void RemoveUser(String userLoginName, string groupName, string SiteURL)
{
	SPSecurity.RunWithElevatedPrivileges(delegate()
	{
		using (SPSite site = new SPSite(SiteURL))
		{
			using (SPWeb web = site.OpenWeb())
			{
				SPGroup group = web.SiteGroups[groupName];
				SPUser userToRemove = web.EnsureUser(userLoginName);
				group.RemoveUser(userToRemove);
				group.Update();
			}
		}
	});
}

For getting Active Directory groups and users check my other post Get Active Directory Groups and Users programmatically

Advertisements

One Response to Get, Add or Remove SharePoint groups and users programmatically

  1. Pingback: Event Receivers in SharePoint 2013 | DL-UAT

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: