Add User To SharePoint Group Programmatically with STSADM and C#

Apart from SharePoint User Interface, We can programmatically add users to SharePoint group or site in these ways:

  • Using STSADM command-line tool to add users 
  • Add users to Sharepoint site/group using C# Object Model

1. Using STSADM command-line tool to add users:

To add users to the SharePoint site, we can use stsadm classic command line tool.  bulk users to add from a CSV file:

  • Add user to a SharePoint group:
    stsadm -o adduser -url “http://sharepoint.crescent.com/sites/marketing” -userlogin crescent\UserName -useremail [email protected] -group “Marketing Members” -username “User Name”
  • To Add a User directly user under “Site Permissions”
    stsadm -o adduser -url “http://sharepoint.crescent.com/sites/marketing” -userlogin crescent\UserName -useremail [email protected] -role “Full Control” -username “User Name”
  • To Add a user as Site collection Administrator:
    stsadm -o adduser -url “http://sharepoint.crescent.com/sites/marketing” -userlogin crescent\UserName -useremail [email protected] -group “Marketing Owners” -username “User Name” -siteadmin

Stsadm -o Adduser operation Technet reference: http://technet.microsoft.com/en-us/library/cc262627.aspx

2. Add Users to SharePoint Programmatically using C# :

Adding users to SharePoint site programmatically: Here is how we can add users to SharePoint site With .Net Object Model C# Code. (This works both in SharePoint 2007 and SharePoint 2010)

using(SPSite site=new SPSite("http://sharepoint.crescent.com"))
{
	using (SPWeb web = site.RootWeb)
	{
		string GroupName="SharePoint Members";
		SPGroup group = web.Groups[GroupName];

		//To Get the Default Members group
		//SPGroup group = web.SiteGroups.Web.AssociatedMemberGroup;

		SPUser user = web.EnsureUser("Crescent\\Salaudeen");
		group.AddUser(user);
		web.Update();
	}
}

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

Leave a Reply