Client side object model is becoming very popular now and learning CSOM is mandatory to access the various resources present in SharePoint Online.
In today’s blog, I will explain how to fetch the groups and users in a site collection that is residing in office 365. We are using CSOM PowerShell scripting to get all the required information.
Hence, let’s get started.
In order to execute this script, we require two important DLL’s (dynamic link library) namely Microsoft.SharePoint.Client and Microsoft.SharePoint.Client.RunTime, so we are using add-type path to load the two DLLs. This is used to add a .NET Framework type of the class to the execute PowerShell session and clear-host will remove any messages in the console Window of Windows PowerShell.
Clear-Host
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"
In the next step, we are declaring a $credential variable to fetch the credentials to access SharePoint Online. This will prompt the user to enter the username and password and the password that we get is converted into the secured string. For security reasons, $url is required to put appropriate SharePoint online URL.
- $credential=Get-Credential
- $ctx=""
- $username=$credential.UserName
- $password=$credential.GetNetworkCredential().Password
- $securepassword=ConvertTo-SecureString $password -AsPlainText -Force
- $url="your site collection url"
- try {
- function GetSourceGroupsandUsers() {
- $grouparray = @()
- $ctx = New - Object Microsoft.SharePoint.Client.ClientContext($url)
- $Credentials = New - Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securepassword)
- $ctx.Credentials = $Credentials
- $web = $ctx.Web
- $ctx.Load($web)
- Write - Host $web.Title
- $ctx.Load($web.SiteGroups)
- $ctx.Load($web.SiteUsers)
- $ctx.ExecuteQuery()[Microsoft.SharePoint.Client.GroupCollection] $groupcollection = $web.SiteGroups
- foreach($group in $groupcollection) {
- $ctx.Load($group)
- $ctx.ExecuteQuery()
- $siteusers = $group.Users
- $ctx.Load($siteusers)
- $ctx.ExecuteQuery()
- foreach($user in $siteusers) {
- $obj = New - Object PSObject
- $login = $user.LoginName
- $obj | Add - Member - MemberType NoteProperty - Name "LoginName"
- $login
- $obj | Add - Member - MemberType NoteProperty - Name "Group Name"
- $group.Title
- $obj | Add - Member - MemberType NoteProperty - Name "Group Id"
- $group.Id
- $obj | Add - Member - MemberType NoteProperty - Name "User Name"
- $user.Title
- $grouparray += $obj
- }
- }
- $grouparray | Export - Csv - Path "your export path"
- }
- GetSourceGroupsandUsers
- }
- Catch {
- $errormessage = $_.Exception.Message
- $errormessage | Out - File "your path for exception logging"
- Write - Host "Some Error Occurred"
- }
To execute this script, one should have the site collection administrator access for the site. Please find the attached script for the reference and the same can be downloaded.
Join the conversation! Your thoughts help the community grow.