Introduction
In this article, you will learn how to get the Site groups and users from the SharePoint site or how to create and add site groups with members using CSOM with PowerShell on SharePoint 2013 / SharePoint online.
Get groups and users
The following section explains in detail about getting the groups and members of SharePoint site.
1.Add the references using the Add-Type command with necessary reference paths. The necessary references are Microsoft.SharePoint.Client.dll, Microsoft.SharePoint.Client.Runtime.dll and Microsoft.SharePoint.Client.Publishing.dll.
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
2. Initialize client context object with the site URL.
$siteURL = ""
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)
3. If you are trying to access SharePoint Online site, then you need to setup the site credentials with credentials parameter and get it set to the client context.
# Not required for on premise site - Start
$userId = ""
$pwd = Read-Host -Prompt "Enter password" -AsSecureString
$creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userId, $pwd)
$ctx.credentials = $creds
# Not required for on premise site - End
4. If you are trying to access the SharePoint on premise site, then the credentials parameter is not required to be set to the context. But you need to run the code on the respective SharePoint server.
5. Get the site groups and site users by loading and executing the query. Here you can retrieve your own custom sub site. Groups will fetch all the groups. SiteUsers method will fetch us all the users who have access to the site.
#$web = $ctx.Site.RootWeb
#To get root site details
$web = $ctx.Site.OpenWeb("/SharePoint")
$groups = $web.SiteGroups # Gets all site groups
$siteUsers = $web.SiteUsers # Gets all users who have access to site in any way
$ctx.Load($groups)
$ctx.Load($siteUsers)
$ctx.ExecuteQuery()
6. Get the site group details and group member details who are all part of each group.
# Gets all the groups foreach($group in $groups){
Write-Host "Site Group : " $group.Title
Write-Host $group.Description
$users = $group.Users
$ctx.Load($users)
$ctx.ExecuteQuery()
Write-Host "Group Members:- "
foreach($user in $users){
Write-Host " " $user.Title }
Write-Host "----------------------"
}
7. Get site users.
# This will display all the users who have access to the site foreach($siteUser in $siteUsers)
{
Write-Host $siteUser.Title
}
Create and Add groups with users
The following section explains in detail about creating and adding the groups with members to SharePoint site.
1. Execute first 5 steps from the above section.
2. Create new group.
Then create new group with Group creation information object with title and descriptions.
Add it to the existing site group collection
Load and execute the query
$newGroupInfo = New-Object Microsoft.SharePoint.Client.GroupCreationInformation
$newGroupInfo.Title = "CustomSPGroup"
$newGroupInfo.Description = "Custom Group"
$newGroup = $web.SiteGroups.Add($newGroupInfo)
$ctx.Load($newGroup)
$ctx.ExecuteQuery()
3. Then add new user to the above group. The following snippets depicts the flow. $userInfo = $web.EnsureUser("abc@abc.onmicrosoft.com")
$ctx.Load($userInfo)
$addUser = $newGroup.Users.AddUser($userInfo)
$ctx.Load($addUser)
$ctx.ExecuteQuery()
4. Add the SharePoint group to the site with necessary information.
Break the root level inheritance permission.
Get the role definition with permission details and add it to site with the group details.
Load, update and execute the query.
$access = $web.RoleDefinitions.GetByName("Read")
$roleAssignment = New- Object Microsoft.SharePoint.Client.RoleDefinitionBindingCollection($ctx)
$roleAssignment.Add($access)
$addPermission = $web.RoleAssignments.Add($newGroup, $roleAssignment)
$web.BreakRoleInheritance($false, $false)
$ctx.Load($web)
$ctx.Load($addPermission)
$web.Update()
$ctx.ExecuteQuery()
The new group has been created along with the members. It has been added to the site with read level permissions.
Summary
Thus you have learned how to retrieve the site users and site groups from the site and how to add groups with users to the site using CSOM with PowerShell commands on SharePoint 2013 / SharePoint online.
Comments