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

[玩转系统] SharePoint Online:使用 PowerShell 获取查找列值

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

SharePoint Online:使用 PowerShell 获取查找列值


要求:使用 PowerShell 在 SharePoint Online 中获取查找字段值。

场景:我们有一个名为“父项目”的父列表和一个名为“项目里程碑”的子列表。子列表中的“父项目名称”字段是从父列表的“项目名称”字段中查找的。这是我的 PowerShell 脚本,用于获取查找字段值。

[玩转系统] SharePoint Online:使用 PowerShell 获取查找列值

PowerShell 在 SharePoint Online 中获取查找字段值:

若要检索 SharePoint Online 列表中查找列的值,请使用此 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 parameter values
$SiteURL="https://crescent.sharepoint.com/Sites/PMO"
$ListName="Project Milestones"
$FieldName="ParentProjectName" #Internal Name
$ListItemID="25"

#Get Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
 
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials

#Get the List Item
$List = $Ctx.Web.lists.GetByTitle($ListName)
$ListItem = $List.GetItemById($ListItemID)
$Ctx.Load($ListItem)
$Ctx.ExecuteQuery()

#Get Lookup Field Value
$Lookup = [Microsoft.SharePoint.Client.FieldLookupValue]$ListItem[$FieldName]
Write-host $Lookup.LookupValue

SharePoint Online:使用 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 parameter values
$SiteURL="https://crescent.sharepoint.com/sites/PMO"
$ListName="Projects"
$FieldName="ParentProjects" #Internal Name
$ListItemID="25"

#Get Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
 
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials

#Get the List Item
$List = $Ctx.Web.lists.GetByTitle($ListName)
$ListItem = $List.GetItemById($ListItemID)
$Ctx.Load($ListItem)
$Ctx.ExecuteQuery()

#Get Multi-Lookup Field Value
$LookupValueCollection = [Microsoft.SharePoint.Client.FieldLookupValue[]]$ListItem[$FieldName]

Foreach ($LookupValue in $LookupValueCollection)
{
    write-host $LookupValue.LookupValue 
}

用于检索查找字段值的 PnP PowerShell

以下是获取查找字段值的 PnP PowerShell 方法。


#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Retail"
$ListName = "Projects"
$LookupFieldName = "Parent_x0020_Project" #Internal Name

#Connect to SharePoint Online
Connect-PnPOnline -URL $SiteURL -Interactive

#Get the first list item
$ListItem = Get-PnPListItem -List $ListName -PageSize 500 | Select -First 1

#Get the Lookup Field value
$ListItem[$LookupFieldName].LookupValue

同样,要从多值查找字段获取所有值,请使用以下命令:


$ListItem[$LookupFieldName].LookupValue -join "; "

这是关于使用 PowerShell 更新 SharePoint Online 中的查找列值的另一篇文章 使用 SharePoint Online 中的 PowerShell 更新查找字段

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

取消回复欢迎 发表评论:

关灯