如何在 SharePoint Online 中更新托管元数据文件 属性

How to update a managed metadata file property in SharePoint Online

我正在尝试为文档库中文件夹中文件的托管元数据列设置 属性 值。我可以为其他元数据设置值,因为它们是文本字段。我相信我必须将它转换为另一种类型才能设置值。我尝试了我发现的各种方法,但似乎没有任何效果。 "Doc Type" 是有问题的托管元数据列。

$files = Get-PnPFolderItem -FolderSiteRelativeUrl $folderRelativeUrl -ItemType File 

foreach($file in $files)
{
    write-output $file.Name
    $file.Properties['Business Unit'] = $Global:BusinessUnit
    $file.Properties['Customer Name'] = $Global:CustomerName
    $file.Properties['Customer No'] = $Global:CustomerNumber
    #$file.Properties['Doc Type'] = $Global:DocType
    $file.Properties['Doc Type'] = $term.Name
    $file.Properties['Job Name'] = $Global:JobName
    $file.Properties['Job Number'] = $Global:JobNumber
    $file.Properties['Opportunity Name'] = $Global:OpportunityName
    $file.Properties['Opportunity No'] = $Global:OpportunityNumber
    $file.Properties['Quote Name'] = $Global:QuoteName
    $file.Properties['Quote ID'] = $Global:QuoteNumber
    $file.Properties['System'] = $Global:System
    $file.Update()

我正在使用此命令从术语库中获取术语

$term = Get-PnpTerm -TermGroup "Document Types" -TermSet Document -Identity $Folder -ErrorAction Ignore

$Folder 是我在术语库中查找的名称,例如:"Quote".

您可以考虑使用以下选项来设置分类值:

Note: it is assumed Doc Type is a single valued taxonomy field

选项 1

来自 CSOM API 的 Taxonomy,如下所示:

$files = Get-PnPFolderItem -FolderSiteRelativeUrl $folderRelativeUrl -ItemType File 

$term = Get-PnpTerm -TermGroup $termGroup -TermSet $termSet -Identity $termId 

#1. get taxonomy specific field
$ctx = $files.Context
$field = $files.ListItemAllFields.ParentList.Fields.GetByInternalNameOrTitle($fieldName) 
$ctx.Load($field)
$ctx.ExecuteQuery()
$taxField = [Microsoft.SharePoint.Client.ClientContext].GetMethod("CastTo").MakeGenericMethod([Microsoft.SharePoint.Client.Taxonomy.TaxonomyField]).Invoke($ctx, $field)

foreach($file in $files)
{
    write-output $file.Name

    $taxField.SetFieldValueByTerm($file.ListItemAllFields,$term,1033) #2.update taxonomy field value
    $file.ListItemAllFields['Job Name'] = $Global:JobName
    $file.ListItemAllFields.Update()
    #$file.Update()    
}

选项 2

通过 PnP-PowerShell SetPnPTaxonomyFieldValue cmdlet:

foreach($file in $files)
{
    write-output $file.Name
    Set-PnPTaxonomyFieldValue -ListItem $file.ListItemAllFields -InternalFieldName 'DocType' -TermId $term.Id        
}