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

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


ข้อกำหนด: อัปเดตฟิลด์การค้นหาโดยใช้ PowerShell ใน SharePoint Online

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

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

เมื่อต้องการอัปเดตค่าคอลัมน์การค้นหาในรายการ 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"

#Function to get the ID from Parent Lookup List -  Based on the Provided value
Function Get-ParentLookupID($ParentListName, $ParentListLookupField, $ParentListLookupValue)
{
    $ParentList = $Ctx.Web.lists.GetByTitle($ParentListName)

    #Get the Parent List Item Filtered by given Lookup Value
    $Query = New-Object Microsoft.SharePoint.Client.CamlQuery
    $Query.ViewXml="<View Scope='RecursiveAll'><Query><Where><Eq><FieldRef Name='$($ParentListLookupField)'/><Value Type='Text'>$($ParentListLookupValue)</Value></Eq></Where></Query></View>"
    $ListItems = $ParentList.GetItems($Query)
    $Ctx.Load($ListItems)
    $Ctx.ExecuteQuery()

    #Get the ID of the List Item
    If($ListItems.count -gt 0)
    {
        Return $ListItems[0].ID #Get the first item - If there are more than One
    }
    else
    {
        Return $Null
    }
}

#Set parameter values
$SiteURL="https://crescent.sharepoint.com/sites/pmo"
$ListItemID="25"
$ParentListName= "Parent Projects" #Lookup Parent List
$ChildListName="Project Milestones" #List to add new lookup value
$ParentListLookupField= "ProjectName" #Internal Name
$ChildListLookupField ="ParentProject" #Internal Name
$ParentListLookupValue="Cloud Development" #Parent Project value

#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 Child List & Item to update
$ChildList = $Ctx.Web.lists.GetByTitle($ChildListName) 
$ListItem = $ChildList.GetItemById($ListItemID)
$Ctx.Load($ListItem)
$Ctx.ExecuteQuery()

#Get the Lookup ID from the Parent List
$LookupID = Get-ParentLookupID $ParentListName $ParentListLookupField $ParentListLookupValue

#Check if the given Lookup Item found in the Parent List
If($LookupID -ne $Null)
{
    #Update Lookup Field using Parent Lookup Item ID
    $ListItem[$ChildListLookupField] = $LookupID
    $ListItem.Update()
    $Ctx.ExecuteQuery()

    Write-host -f Green "Lookup Field value has been updated!"
}
else
{
    write-host -f Yellow "Lookup Item '$($ParentListLookupValue)' doesn't exist in the Parent List!"
} 

PowerShell เพื่ออัปเดตค่าคอลัมน์การค้นหาแบบหลายค่า

นี่คือตัวอย่าง PowerShell สำหรับการตั้งค่าคอลัมน์การค้นหาแบบหลายค่าใน SharePoint Online:


#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"

#Function to get the ID from Parent Lookup List -  Based on the Provided value
Function Get-ParentLookupID($ParentListName, $ParentListLookupField, $ParentListLookupValue)
{
    $ParentList = $Ctx.Web.lists.GetByTitle($ParentListName)

    #Get the Parent List Item Filtered by given Lookup Value
    $Query = New-Object Microsoft.SharePoint.Client.CamlQuery
    $Query.ViewXml="<View Scope='RecursiveAll'><Query><Where><Eq><FieldRef Name='$($ParentListLookupField)'/><Value Type='Text'>$($ParentListLookupValue)</Value></Eq></Where></Query></View>"
    $ListItems = $ParentList.GetItems($Query)
    $Ctx.Load($ListItems)
    $Ctx.ExecuteQuery()

    #Get the ID of the List Item
    If($ListItems.count -gt 0)
    {
        Return $ListItems[0].ID #Get the first item - If there are more than One
    }
    else
    {
        Return $Null
    }
}

#Set parameter values
$SiteURL="https://crescent.sharepoint.com/sites/pmo"
$ListItemID="25"
$ParentListName= "Parent Projects" #Lookup Parent List
$ChildListName="Project Milestones" #List to add new lookup value
$ParentListLookupField= "ProjectName"
$ChildListLookupField ="ParentProject"
$ParentListLookupValues=@("Address Books", "Holidays", "Invoices") #Parent Project value

#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 Child List & Item to update
$ChildList = $Ctx.Web.lists.GetByTitle($ChildListName) 
$ListItem = $ChildList.GetItemById($ListItemID)
$Ctx.Load($ListItem)
$Ctx.ExecuteQuery()

$LookupValueCollection = @()
ForEach($ParentListLookupValue in $ParentListLookupValues)
{
    #Get the Lookup ID from the Parent List
    $LookupID = Get-ParentLookupID $ParentListName $ParentListLookupField $ParentListLookupValue

    #Check if the given Lookup Item found in the Parent List
    If($LookupID -ne $Null)
    {
        $lookupValue = New-Object Microsoft.SharePoint.Client.FieldLookupValue
        $lookupValue.LookupId = $LookupID
        $LookupValueCollection += $LookupValue
    }
    else
    {
        write-host -f Yellow "Lookup Item '$($ParentListLookupValue)' doesn't exist in the Parent List!"
    }
}

#sharepoint online PowerShell update lookup field
If($LookupValueCollection.length -gt 0)
{
    #Convert the array to Lookup value collection
    $LookupValueColl = [Microsoft.SharePoint.Client.FieldLookupValue[]]$LookupValueCollection 

    #update lookup field using powershell in sharepoint online
    $ListItem[$ChildListLookupField] = $LookupValueColl
    $ListItem.Update()
    $Ctx.ExecuteQuery()
    Write-host -f Green "Multi-Lookup Field value has been updated!"
}

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

ฟิลด์การค้นหาจะจัดเก็บ ID ของรายการการค้นหาหลักไว้ภายใน เมื่อต้องการอัปเดตค่าฟิลด์การค้นหาใน SharePoint Online ให้ใช้ค่า @{“Lookup”=“1”} โดยที่ “1” คือ ID ของรายการการค้นหาหลัก


#Config Variables
$SiteURL = "https://Crescent.sharepoint.com/sites/marketing"
$ListName = "ProjectMilestones"
$FieldName = "Parent_x0020_Project"
$ItemID = 1
 
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
 
#Update lookup field value using PnP PowerShell
Set-PnPListItem -List $ListName -Identity $ItemID -Values @{$FieldName = 3}

ที่นี่ "3" คือ ID ของรายการค้นหาหลัก ในทำนองเดียวกัน หากต้องการอัปเดตฟิลด์การค้นหาที่มีหลายค่า เพียงระบุ ID ของรายการค้นหาหลักโดยคั่นด้วยเครื่องหมายจุลภาค

SharePoint Online: อัปเดตค่าฟิลด์การค้นหาโดยใช้ PnP PowerShell

เราอาจไม่ทราบ ID ของรายการค้นหาหลักเสมอไป และอาจต้องการตั้งค่าฟิลด์การค้นหาตามค่าข้อความ ต่อไปนี้คือวิธีที่เราสามารถอัปเดตค่าฟิลด์การค้นหาใน SharePoint Online ด้วยค่าข้อความการค้นหา


#Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/pmo"
$ListName = "Project Milestones"
$LookupFieldName = "Parent_x0020_Project"
$LookupValueText = "VMware Upgrade"
$ItemID = 1

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

#Get Parent Lookup List and Field from Child Lookup Field's Schema XML
$LookupField =  Get-PnPField -List $ListName -Identity $LookupFieldName
[Xml]$Schema = $LookupField.SchemaXml
$ParentListID = $Schema.Field.Attributes["List"].'#text'
$ParentField  = $Schema.field.Attributes["ShowField"].'#text'
$ParentLookupItem  = Get-PnPListItem -List $ParentListID -Fields $ParentField | Where {$_[$ParentField] -eq $LookupValueText} | Select -First 1

If($ParentLookupItem -ne $Null)
{
    #Update lookup field value using PnP PowerShell
    Set-PnPListItem -List $ListName -Identity $ItemID -Values @{$LookupFieldName = $ParentLookupItem["ID"]}
    Write-host "Lookup Column Value Updated Successfully!" -f Green
}
Else
{
    Write-host "Lookup Column Value '$LookupValueText' Not found in the Parent Lookup List!" -f Yellow
}

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

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