Nhận/Đặt giá trị trường tra cứu trong Danh sách SharePoint bằng PowerShell
Đây là đoạn mã PowerShell của tôi để đọc/ghi các giá trị trường tra cứu trong danh sách SharePoint.
Tình huống: Hãy xem xét bạn có một danh sách chính có tên là “Dự án gốc” và danh sách con “Các cột mốc dự án”. Trường “Tên dự án gốc” từ danh sách con đang được tra cứu từ trường “Tên dự án” của danh sách gốc.
Nhận giá trị trường tra cứu từ danh sách SharePoint bằng PowerShell:
Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue
#Variables
$WebURL="https://projects.crescent.com"
$ListName="Project Milestones"
$LookupFieldName="Parent Project"
#Get Web and List objects
$Web = Get-SPWeb $WebURL
$List = $Web.Lists.tryGetList($ListName)
if($List -ne $null)
{
#Get a Row from list
$Item = $List.Items[0]
#Get Lookup Field Value
$Lookup = New-Object Microsoft.SharePoint.SPFieldLookupValue($Item[$LookupFieldName])
#Send the output to screen
write-host $Lookup.LookupValue
}
Đọc từ Giá trị trường tra cứu nhiều giá trị:
Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue
#Variables
$WebURL="https://projects.crescent.com"
$ListName="Project Milestones"
$LookupFieldName="Parent Project"
#Get Web and List objects
$Web = Get-SPWeb $WebURL
$List = $Web.Lists.tryGetList($ListName)
if($List -ne $null)
{
#Get a Row from list
$Item = $List.Items[0]
#Get Lookup Field Value
$LookupValueCollection = New-Object Microsoft.SharePoint.SPFieldLookupValueCollection($Item[$LookupFieldName].ToString())
Foreach ($LookupValue in $LookupValueCollection)
{
write-host $LookupValue.LookupValue
}
}
Làm cách nào để cập nhật cột tra cứu trong SharePoint bằng PowerShell?
Để cập nhật cột tra cứu, bạn chỉ cần sử dụng: $ListItem[LookupFieldName]=”Parent-Item-ID;#LookupValue”
Ví dụ: Giả sử bạn có cột tra cứu có tên “Tên dự án gốc” trong danh sách “Số liệu dự án”, tham chiếu trường “Tên dự án” từ danh sách “Dự án gốc” như trong hình trên. Bây giờ, để đặt cột tra cứu, bạn có thể sử dụng:
$ProjectMetrics["ParentProjectName"]="2;#Cloud Devlopment"
Trong đó: 2 là ID của hàng trong danh sách Dự án gốc trong đó Tên dự án là: Phát triển đám mây.
PowerShell để đặt trường tra cứu trong SharePoint:
Dưới đây là cách cập nhật trường tra cứu trong SharePoint bằng PowerShell
Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue
#Variables
$WebURL="https://projects.crescent.com"
$ParentListName="Parent Projects" #Lookup Parent List
$ChildListName="Project Milestones" #List to add new lookup value
$ParentListLookupField="Project Name"
$ChildListLookupField ="Parent Project"
$ParentListLookupValue="Cloud Development" #Parent Project value
#Get Web and List objects
$Web = Get-SPWeb $WebURL
$ParentList = $Web.Lists.tryGetList($ParentListName)
$ChildList = $Web.Lists.tryGetList($ChildListName)
#Add new item in Child List - Project Milestones
$NewItem=$ChildList.Items.Add()
#Set Title field for the new list item
$NewItem["Title"]="Dec 2014 Milestone"
#Get Lookup Item from Parent List
$ParentListLookupItem = $ParentList.Items | where {$_[$ParentListLookupField] -eq $ParentListLookupValue}
#Set Lookup Field Value in Child List
$NewItem[$ChildListLookupField] = $ParentListLookupItem.ID
$NewItem.Update()
#Send the output to screen
write-host "Lookup Field value has been updated!"
Cập nhật cột Tra cứu nhiều giá trị bằng PowerShell:
Còn việc cập nhật trường tra cứu khi bật “Cho phép nhiều giá trị” thì sao?
Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue
#Variables
$WebURL="https://projects.crescent.com"
#Get Web and List objects
$Web = Get-SPWeb $WebURL
$ParentProjects = $Web.Lists.tryGetList("Parent Projects")
$ProjectMileStones = $Web.Lists.tryGetList("Project Milestones")
#Add new item in Child List - Project Milestones
$NewMilestone=$ProjectMileStones.Items.Add()
#Set Title field for the new list item
$NewMilestone["Title"]="Dec 2014 Milestone"
#Object for MultiValue Lookup Field Values
$LookupValues = New-Object Microsoft.SharePoint.SPFieldLookupValueCollection
#Get a Lookup Item from Parent List
$ParentProjectItem = $ParentProjects.Items | where {$_["Project Name"] -eq "Cloud Development"}
$LookupValue = New-Object Microsoft.SharePoint.SPFieldLookupValue
$LookupValue.LookupId = $ParentProjectItem.ID
$LookupValues.Add($LookupValue) #Add to Collection
#Get an another Lookup Item from Parent List
$ParentProjectItem = $ParentProjects.Items | where {$_["Project Name"] -eq "Smart Home"}
$LookupValue = New-Object Microsoft.SharePoint.SPFieldLookupValue
$LookupValue.LookupId = $ParentProjectItem.ID
$LookupValues.Add($LookupValue) #Add to Collection
#Set multi-value lookup field value
$NewMilestone["Parent Project"] = $LookupValues
$NewMilestone.Update()
Để nhận hoặc đặt giá trị cột Tra cứu trong SharePoint Online, hãy sử dụng bài đăng khác của tôi: PowerShell để Truy xuất/Cập nhật Giá trị Cột Tra cứu trong SharePoint Online

