【问题标题】:SharePoint Online & PNP PowerShell - output a libraries documents to CSV within all metadata, including CONTENT TYPESharePoint Online 和 PNP PowerShell - 在所有元数据中将库文档输出到 CSV,包括内容类型
【发布时间】:2020-11-24 20:50:17
【问题描述】:

我一直在使用以下内容以 CSV 格式输出 SharePoint Online 文档库,包括所有自定义元数据字段属性。

但是我无法获得指定的内容类型。我将其视为标准列,但想象它还有更多内容吗?我似乎找不到任何相关内容。

#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!"

欢迎任何关于我如何捕获所涉及的库项目的内容类型的指针!

【问题讨论】:

  • 也许你用错了名字,如果你运行Get-PnPField -List $List | Where-Object Title -eq 'ContentType'会看到什么?

标签: powershell sharepoint-online


【解决方案1】:

试试这个:

$ctx = Get-PnPContext
Foreach ($Item in $ListItems)
 {
$Ctx.Load($Item.ContentType)
$Ctx.ExecuteQuery()
 write-host $Item.ContentType.Name
 }

测试结果:

更新:

 $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
        })

【讨论】:

  • 谢谢阿莫斯。运行您的建议确实列出了内容类型。我正在努力将解决方案合并到我的脚本中,以将内容类型值作为列与其他元数据一起使用 - 你对此有什么建议吗?谢谢!
  • 我没有找到名称为内容类型的列表列。你可以像这样组合两段代码,请看我的更新。
猜你喜欢
  • 2017-09-25
  • 2016-07-05
  • 2021-12-13
  • 1970-01-01
  • 2021-12-12
  • 1970-01-01
  • 1970-01-01
  • 2018-12-04
  • 2020-09-07
相关资源
最近更新 更多