Friday, April 29, 2016

SharePoint Online: Add User to Group using PowerShell-CSOM

How to add people to existing groups in SharePoint online:

  • Login to SharePoint online site >> Click on Site Settings >> Under Site Settings page, in the Users And Permissions section, click on "Site Permissions" link.
  • On the Permissions page, click on "Grant Permissions" button in the ribbon from Permissions tab.
    SharePoint Online PowerShell to Add User to Group
  • In the Share dialog box, enter name / email addresses
  • Click on "Hide Options" link button. In the Select A Group Or Permission Level list box, select the appropriate group name such as "Members"
    sharepoint online add user to group powershell
  • Click Share button.

SharePoint Online PowerShell to Add User to Group
Here is how we can add user to group using PowerShell in SharePoint Online.
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
 
#Variables for Processing
$SiteURL = "https://crescent.sharepoint.com/Sites/Sales"
$UserAccount = "Salaudeen@crescent.com"
$GroupName="Sales Members"

#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Cred
    
    #Get the Web and Group
    $Web = $Ctx.Web
    $Group= $Web.SiteGroups.GetByName($GroupName)

    #Resolve the User
    $User=$web.EnsureUser($UserAccount)

    #Add user to the group
    $Result = $Group.Users.AddUser($User)
    $Ctx.Load($Result)
    $Ctx.ExecuteQuery()

    write-host  -f Green "User '$UserAccount' has been added to '$GroupName'"
}
Catch {
    write-host -f Red "Error Adding user to Group!" $_.Exception.Message
}
You can also use Add-SPOUser using SharePoint online management shell : SharePoint Online: PowerShell to Add Users to Group



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!


You might also like:

Related Posts Plugin for WordPress, Blogger...