Quantcast
Channel: SharePoint Diary
Viewing all articles
Browse latest Browse all 1058

How to Add User To SharePoint Site Group Programmatically

$
0
0
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
  • Add users to SharePoint Group using PowerShell

1. Using STSADM command line tool to add users:
To add user 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 UserName@crescent.com -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 UserName@crescent.com -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 UserName@crescent.com -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();
                    }
 }

3. Add User to SharePoint 2010 using PowerShell Cmd-lets:
New-SPUser:
New-SPUser -UserAlias "domain\user" -Web "http://sharepoint.crescent.com/sites/marketing" -Group "Marketing Owners"
This will create a add a new User to SharePoint site to the particular group. If you execute this command for the next time, (without deleting the user from site collection) this command has no effect!

Set-SPUser:
Set-SPUser -Identity "domain\user" -Web "http://sharepoint.crescent.com/sites/marketing" -Group "Marketing Owners"
This will add existing SharePoint users account to the provided group, but will give error when you try add a new user to SharePoint site. (which is obvious! We can't set the user property, if the user doesn't exists in SharePoint site, isn't it?)

Add User to SharePoint Group using PowerShell

#Get the Web
$web=Get-SPWeb "http://sharepoint.crescent.com/sites/marketing"
#Get the Group
$Group= $web.Groups["Marketing Owners"]
$userName = "domain\userName"

#Add User to the site collection
$user = $web.EnsureUser($UserName)

#Add User to the Group
$group.AddUser($user)


Import Bulk Users to SharePoint site from CSV file using PowerShell:

Got bunch of users and want to add them in to SharePoint? My CSV file has AccountName (in the format of: Domain\UserName and GroupName).  To bulk add users to SharePoint group programmatically using PowerShell, use the below script:
add user to group powershell sharepoint 2010
Powershell script to add user to SharePoint group:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Import the List of Users from a .csv file,
$UserList= Import-CSV "C:\UserList.csv"
 
#Get the Web
$web = Get-SPWeb "http://sharepoint.crescent.com/sites/marketing"
 
foreach ($Row in $UserList)
{
  #Get the Group
   $Group = $web.Groups[$Row.GroupName]
 
 #Validate the user name
 try
   {
        $user = $web.Site.RootWeb.EnsureUser($Row.AccountName)
   }
   catch [system.exception]
   {
     write-host $_.Exception.Message
   }
 
   #Add user if valid user name is provided
    if($user -ne $null)
    {
       $Group.AddUser($user)
       write-host $user.AccountName
    }
}

#Dispose the web object
$Web.Dispose()

Viewing all articles
Browse latest Browse all 1058

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>