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"
Now, we are writing a function to fetch the required details. We are including the code block inside try and catch, as it is a best coding practice. We are getting context of Sharepoint Online site, using ClientContext Object. By passing URL, we are setting the credentials to SharePointOnlineCredentails Object by passing the username and secured password. Now, $web will get root site. $ctx.load will load the $web object in to the client context and we are getting SiteGroups, SiteUsers from the $web and loading it in to the client context. Use context.executequery() to complete the load action to get all the site groups in a variable of type GroupCollection Object.
- 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
Now, we are iterating through each group and loading it. By fetching all the users for the group and for each of the users in a group, we are fetching the login name, group name, group Id and user name. This information is collected through PS Object with various members (PSObject is similar to any other object and members are similar to properties of an object) and finally we are adding each object to $grouparray variable. Afterwards, we are using export-csv cmdlet, which will convert the array of the objects to a CSV file and path is where CSV file is saved.
- 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"
- }
We are calling the function GetSourceGroupsandUsers and finally ending the code try block with catch block. Any error message can be logged in, using out-file and provide the path to save the log file.
- GetSourceGroupsandUsers
- }
- Catch {
- $errormessage = $_.Exception.Message
- $errormessage | Out - File "your path for exception logging"
- Write - Host "Some Error Occurred"
- }
Note
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.