Reputation: 11
I have been using the following to output as a CSV a SharePoint Online document library, including all custom metadata field properties.
I however cannot obtain the assigned Content Type. I am treating as a standard column, but imagine there is more to it? I don't seem to be able to find anything relevant.
#Parameters
$SiteURL = "https://SHAREPOINT URL"
$ListName= "LIBRARYNAME"
$ReportOutput = "C:\LOCATION.csv"
$Pagesize = 500
#Connect to SharePoint Online site
Connect-PnPOnline $SiteURL -UseWebLogin
#Delete the Output report file if exists
If (Test-Path $ReportOutput) { Remove-Item $ReportOutput}
#Array to store results
$Results = @()
#Get all Documents from the document library
$List = Get-PnPList -Identity $ListName
$global:counter = 0;
$ListItems = Get-PnPListItem -List $ListName -PageSize $Pagesize -Fields Author, Editor, Created, File_x0020_Type, Business_x0020_Unit, Department, Device, Document_x0020_Type, Employee_x0020_Status, Retention_x0020_Period, Scan_x0020_Date, Scanned_x0020_by, Staff_x0020_ID, Staff_x0020_Name, ContentType -ScriptBlock `
{ Param($items) $global:counter += $items.Count; Write-Progress -PercentComplete ($global:Counter / ($List.ItemCount) * 100) -Activity `
"Getting Documents from Library '$($List.Title)'" -Status "Getting Documents data $global:Counter of $($List.ItemCount)";} | Where {$_.FileSystemObjectType -eq "File"}
$ItemCounter = 0
#Iterate through each item
Foreach ($Item in $ListItems)
{
$Results += New-Object PSObject -Property ([ordered]@{
Name = $Item["FileLeafRef"]
Type = $Item.FileSystemObjectType
FileType = $Item["File_x0020_Type"]
RelativeURL = $Item["FileRef"]
CreatedByEmail = $Item["Author"].Email
CreatedOn = $Item["Created"]
Modified = $Item["Modified"]
ModifiedByEmail = $Item["Editor"].Email
BusinessUnit = $Item["Business_x0020_Unit"]
Department = $Item["Department"]
Device = $Item["Device"]
DocumentType = $Item["Document_x0020_Type"]
EmployeeStatus = $Item["Employee_x0020_Status"]
RetentionPeriod = $Item["Retention_x0020_Period"]
ScanDate = $Item["Scan_x0020_Date"]
ScannedBy = $Item["Scanned_x0020_by"]
StaffID = $Item["Staff_x0020_ID"]
StaffName = $Item["Staff_x0020_Name"]
ContentType = $Item["ContentType"]
})
$ItemCounter++
Write-Progress -PercentComplete ($ItemCounter / ($List.ItemCount) * 100) -Activity "Exporting data from Documents $ItemCounter of $($List.ItemCount)" -Status "Exporting Data from Document '$($Item['FileLeafRef'])"
}
#Export the results to CSV
$Results | Export-Csv -Path $ReportOutput -NoTypeInformation
Write-host "Document Library Inventory Exported to CSV Successfully!"
Any pointers on how I can capture the involved Content Type of the library items welcomed!
Upvotes: 1
Views: 1281
Reputation: 2091
Try this:
$ctx = Get-PnPContext
Foreach ($Item in $ListItems)
{
$Ctx.Load($Item.ContentType)
$Ctx.ExecuteQuery()
write-host $Item.ContentType.Name
}
Test result:
Updated:
$ctx = Get-PnPContext
Foreach ($Item in $ListItems)
{
$Ctx.Load($Item.ContentType)
$Ctx.ExecuteQuery()
$Results += New-Object PSObject -Property ([ordered]@{
Name = $Item["FileLeafRef"]
Type = $Item.FileSystemObjectType
FileType = $Item["File_x0020_Type"]
RelativeURL = $Item["FileRef"]
CreatedByEmail = $Item["Author"].Email
CreatedOn = $Item["Created"]
Modified = $Item["Modified"]
ModifiedByEmail = $Item["Editor"].Email
BusinessUnit = $Item["Business_x0020_Unit"]
Department = $Item["Department"]
Device = $Item["Device"]
DocumentType = $Item["Document_x0020_Type"]
EmployeeStatus = $Item["Employee_x0020_Status"]
RetentionPeriod = $Item["Retention_x0020_Period"]
ScanDate = $Item["Scan_x0020_Date"]
ScannedBy = $Item["Scanned_x0020_by"]
StaffID = $Item["Staff_x0020_ID"]
StaffName = $Item["Staff_x0020_Name"]
ContentType = $Item.ContentType.Name
})
Upvotes: 1