当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] SharePoint Online:使用 PowerShell 获取托管元数据字段值

作者:精品下载站 日期:2024-12-14 15:40:36 浏览:12 分类:玩电脑

SharePoint Online:使用 PowerShell 获取托管元数据字段值


要求:使用 PowerShell 获取 SharePoint Online 中的托管元数据字段值。

SharePoint Online:用于获取托管元数据字段值的 PowerShell

托管元数据用于在整个组织中提供一致的术语结构。托管元数据字段在内部存储术语 GUID 作为其值。在本文中,我们将介绍如何在 SharePoint Online 中使用 PowerShell 检索托管元数据字段的值。

[玩转系统] SharePoint Online:使用 PowerShell 获取托管元数据字段值

要获取托管元数据列的值,请使用以下 PowerShell:


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
 
#Set parameters
$SiteURL="https://crescent.sharepoint.com/Sites/pmo"
$ListName="Projects" 
$FieldName="Classification" #Internal Name
$ListItemID="1"

#Get Credentials to connect
$Cred = Get-Credential
  
#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 List Item
$List = $Ctx.Web.lists.GetByTitle($ListName)
$ListItem = $List.GetItemById($ListItemID)
$Ctx.Load($ListItem)
$Ctx.ExecuteQuery()
 
#Get Managed Metadata Field Value
$MMSFieldValue = $ListItem[$FieldName]
Write-host $MMSFieldValue.Label
Write-host $MMSFieldValue.TermGuid

PnP PowerShell 获取托管元数据列值

同样,要检索 MMS 列值的值,请使用此 PnP PowerShell。例如,要检索 SharePoint Online 网站中名为“项目”的列表中名为“分类”的字段的值,请使用以下脚本:


#Parameters
$SiteURL="https://crescent.sharepoint.com/Sites/pmo"
$ListName="Projects" 
$FieldName="Classification" #Internal Name
$ListItemID="1"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive

#Get List items from the list
$ListItem = Get-PnPListItem -List $ListName -Id $ListItemID -Fields $FieldName

#Get Managed Metadata Field Value
$MMSFieldValue = $ListItem[$FieldName]
Write-host $MMSFieldValue.Label
Write-host $MMSFieldValue.TermGuid

使用 PowerShell 检索允许多个值的托管元数据字段值:

要获取多值 MMS 字段值,请使用:


#Parameters
$SiteURL="https://crescent.sharepoint.com/Sites/pmo"
$ListName="Projects" 
$FieldName="Classification" #Internal Name
$ListItemID="1"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive

#Get an item from the list
$ListItem = Get-PnPListItem -List $ListName -Id $ListItemID -Fields $FieldName

#Get the Field
$Field = Get-PnPField -List $ListName -Identity $FieldName 

#Check if the field allows Multiple values
If($Field.TypeAsString -eq "TaxonomyFieldType")
{
    #Get Managed Metadata Field Value
    $MMSFieldValue = $ListItem[$FieldName]
    Write-host $MMSFieldValue.Label
    Write-host $MMSFieldValue.TermGuid
}
Else #TaxonomyFieldTypeMulti
{
    $MMSFieldValue = $ListItem[$FieldName] | ForEach-Object {
        Write-host $_.Label
        Write-host $_.TermGuid
    }
}

总之,检索 SharePoint Online 中托管元数据字段的值是一个简单的过程,可以使用 CSOM 或 PnP PowerShell 脚本 cmdlet 来完成。通过遵循本文中提供的示例,您应该能够检索托管元数据字段的值并使用它对 SharePoint Online 中的内容进行分类和组织。

要更新托管元数据字段值,请使用:SharePoint Online:PowerShell 更新托管元数据字段值

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯