[PS/CSOM] List the sensitivity labels of files in a document library
So, here goes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
Import-Module Microsoft.Online.SharePoint.PowerShell Connect-SPOService -Url https://arachita1-admin.sharepoint.com #Load SharePoint CSOM Assemblies Add-Type -Path "C:\CSOM\Microsoft.SharePoint.Client.dll" Add-Type -Path "C:\CSOM\Microsoft.SharePoint.Client.Runtime.dll" #Config Parameters $tenant=https://arachita1.sharepoint.com $SiteURL= https://arachita1.sharepoint.com/sites/Luca-ListIssue $LibraryName = "TESTLIB" #Get Credentials to connect $Cred = Get-Credential Try { #Setup the context $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL) $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password) #Get the Web and List $Web = $Ctx.Web $Ctx.Load($Web) $List = $Web.Lists.GetByTitle($LibraryName) $Ctx.Load($List) $Ctx.ExecuteQuery() #Prepare the query $Query = New-Object Microsoft.SharePoint.Client.CamlQuery $Query.ViewXml = "@ <View Scope='RecursiveAll'> <Query> <OrderBy><FieldRef Name='ID' Ascending='TRUE'/></OrderBy> </Query> <RowLimit Paged='TRUE'>2000</RowLimit> </View>" $Counter=1 $DataCollection = @() #Batch Process items Do { #powershell sharepoint online list all documents $ListItems = $List.GetItems($Query) $Ctx.Load($ListItems) $Ctx.ExecuteQuery() $Query.ListItemCollectionPosition = $ListItems.ListItemCollectionPosition #Iterate through each document in the library ForEach($ListItem in $ListItems| Where {$_.FileSystemObjectType -eq "File"}) { #Display a Progress bar Write-Progress -Activity "Scanning Files in the Library" -Status "Collection file's Inventory '$($Item.FieldValues.FileRef)' ($Counter of $($List.ItemCount))" -PercentComplete (($Counter / $List.ItemCount) * 100) #Collect data $DataCollection += New-Object PSObject -Property ([Ordered] @{ FileName = $ListItem.FieldValues["FileLeafRef"] RelativeURL = $ListItem.FieldValues["FileRef"] CreatedBy = $ListItem.FieldValues["Created_x0020_By"] CreatedOn = $ListItem.FieldValues["Created"] ModifiedBy = $ListItem.FieldValues["Modified_x0020_By"] ModifiedOn = $ListItem.FieldValues["Modified"] FileSize = $ListItem.FieldValues["File_x0020_Size"] }) $Counter++ } }While($Query.ListItemCollectionPosition -ne $Null) #Display results $DataCollection } Catch { write-host -f Red "Error:" $_.Exception.Message } foreach ($file in $DataCollection.Filename) { Write-Host $file $fileurl=$SiteURL+"/"+$LibraryName+"/"+$file get-FileSensitivityLabelInfo -FileUrl $fileurl } |
Sample output: