ค้นหาเว็บไซต์

SharePoint Online: รับค่าคอลัมน์การค้นหาโดยใช้ PowerShell


ความต้องการ: รับค่าฟิลด์การค้นหาใน SharePoint Online โดยใช้ PowerShell

สถานการณ์: เรามีรายการหลักที่เรียกว่า "โครงการหลัก" และรายการย่อยที่เรียกว่า "เหตุการณ์สำคัญของโครงการ" ฟิลด์ "ชื่อโครงการหลัก" ในรายการย่อยจะถูกค้นหาจากฟิลด์ "ชื่อโครงการ" ของรายการหลัก นี่คือสคริปต์ PowerShell ของฉันเพื่อรับค่าฟิลด์การค้นหา

PowerShell เพื่อรับค่าฟิลด์การค้นหาใน SharePoint Online:

เมื่อต้องการดึงค่าของคอลัมน์การค้นหาในรายการ SharePoint Online ให้ใช้ PowerShell นี้


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\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\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\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 "; "

นี่คืออีกโพสต์เกี่ยวกับการอัปเดตค่าคอลัมน์การค้นหาใน SharePoint Online โดยใช้ PowerShell Update Lookup Field โดยใช้ PowerShell ใน SharePoint Online

บทความที่เกี่ยวข้อง