How to deal with permissions in SharePoint using the Client Side Object Model and PowerShell
To get articles like this free in your inbox, subscribe to our newsletter.
This time I am sharing a PowerShell script about how to get the list of all SharePoint Groups and all the users per group in a SharePoint site using the Client Side Object Model in a PowerShell script:
$host.Runspace.ThreadOptions = “ReuseThread”
#Definition of the function that gets all the SharePoint groups and Users per Group in a SharePoint site
function Get-SPAllSharePointUsersInGroups
{
param ($sSiteColUrl,$sUserName,$sDomain,$sPassword)
try
{
Write-Host “—————————————————————————-” -foregroundcolor Green
Write-Host “Getting all Groups in a SharePoint Site” -foregroundcolor Green
Write-Host “—————————————————————————-” -foregroundcolor Green
#Adding the Client OM Assemblies
Add-Type -Path “C:\Scripts PS2 Office 365\Microsoft.SharePoint.Client.dll”
Add-Type -Path “C:\Scripts PS2 Office 365\Microsoft.SharePoint.Client.Runtime.dll”
#SPO Client Object Model Context
$spCtx = New-Object Microsoft.SharePoint.Client.ClientContext($sSiteColUrl)
$spCredentials = New-Object System.Net.NetworkCredential($sUserName,$sPassword,$sDomain)
$spCtx.Credentials = $spCredentials
#Root Web Site
$spRootWebSite = $spCtx.Web
#Collecction of Sites under the Root Web Site
$spSites = $spRootWebSite.Webs
#Loading Operations
$spGroups=$spCtx.Web.SiteGroups
$spCtx.Load($spGroups)
$spCtx.ExecuteQuery()
#We need to iterate through the $spGroups Object in order to get individual Group information
foreach($spGroup in $spGroups){
$spCtx.Load($spGroup)
$spCtx.ExecuteQuery()
Write-Host “* ” $spGroup.Title
#Getting the users per group in the SPO Site
$spSiteUsers=$spGroup.Users
$spCtx.Load($spSiteUsers)
$spCtx.ExecuteQuery()
foreach($spUser in $spSiteUsers){
Write-Host ” -> User:” $spUser.Title ” – User ID:” $spUser.Id ” – User E-Mail:” $spUser.Email ” – User Login:” $spUser.LoginName
}
}
$spCtx.Dispose()
}
catch [System.Exception]
{
write-host -f red $_.Exception.ToString()
}
}
#Required Parameters
$sSiteColUrl = “http://<Site_Url>”
$sUserName = “<UserName>”
$sDomain=”<Domain>”
$sPassword =”<UserPassword>”
Get-SPAllSharePointUsersInGroups -sSiteColUrl $sSiteColUrl -sUserName $sUsername -sDomain $sDomain -sPassword $sPassword
As you can see, the script defines a function that uses the CSOM as the foundations to get all the users per SharePoint group in a SharePoint Online site. The function uses standard SPO objects (ClientContext, Web, SiteGroups, Users) to get all the users in all the SharePoint Groups in a SharePoint site. You can download the PowerShell script from the following download page in the Script Center Gallery in TechNet: How to get all the Users and Groups in a SharePoint Sites using CSOM.
Happy new year and happy CloudSharing!
What you should do next…
1. Subscribe to our newsletter:
Subscribe to our newsletter below for the latest news, advice and thought-leadership for software professionals. Or visit our blog to browse our most recent articles.
2. Learn how virtual labs can grow your business:
To learn more about how CloudShare helps software organizations grow revenue, increase efficiency and improve quality, visit our resources page. You’ll be able to browser dozens of valuable white papers, eBooks, webinars, case studies, and brochures.
3. Get a FREE, no obligation demo:
Discover just how easy it is to create your cloud environment—in minutes! One of our friendly virtual labs experts will be happy to:
- Show you the platform in action
- Calculate pricing for your business
- Set you up with a 14-day free trial
- Answer any questions you have
- No pressure or obligation