I have a requirement to get word or excel file sensitivity label from a sharepoint online document library using CSOM.
How to achieve this. Can anyone help me on this.
Have a look at this snippet:
string siteUrl = "https://yourdomain.sharepoint.com/sites/yoursite";
string fileUrl = "/sites/yoursite/Shared Documents/yourfile.docx";
ClientContext context = new ClientContext(siteUrl);
// Authenticate with your credentials (or use another method to authenticate)
context.Credentials = new SharePointOnlineCredentials("[email protected]", GetSecureString("yourpassword"));
// Get the file from the document library
File file = context.Web.GetFileByServerRelativeUrl(fileUrl);
ListItem listItem = file.ListItemAllFields;
// Load the list item
context.Load(listItem);
context.ExecuteQuery();
// Retrieve the ComplianceTag (Sensitivity Label) and Retention Label
string sensitivityLabel = listItem["ComplianceTag"] != null ? listItem["ComplianceTag"].ToString() : "No Sensitivity Label applied.";
string retentionLabel = listItem["RetentionLabel"] != null ? listItem["RetentionLabel"].ToString() : "No Retention Label applied.";
Here is a Powershell version. I have seen in some places a ComplianceAssetId, so you may need to check all listItem properties to make sure you are using the right one:
$siteUrl = "https://yourdomain.sharepoint.com/sites/yoursite"
Connect-PnPOnline -Url $siteUrl -UseWebLogin
# Path to the file in the document library
$fileServerRelativeUrl = "/sites/yoursite/Shared Documents/yourfile.docx"
# Get the file
$file = Get-PnPFile -Url $fileServerRelativeUrl -AsListItem
# Get the sensitivity label (ComplianceAssetId)
$sensitivityLabel = $file["ComplianceAssetId"]
You could try below codes.
Import-Module Microsoft.Online.SharePoint.PowerShell
Connect-SPOService -Url https://tenant-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://tenant.sharepoint.com
$SiteURL= https://tenant.sharepoint.com/sites/emilytest
$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
}