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

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


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

SharePoint Online PowerShell เพื่ออัปเดตฟิลด์ไฮเปอร์ลิงก์

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

มาเพิ่มรายการลิงก์ใหม่ลงในรายการลิงก์โดยใช้ PowerShell:


#Load SharePoint Online 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"

#Variables
$SiteURL="https://crescent.sharepoint.com"
$ListName = "Quick Links"
$FieldName = "URL"

#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
$List = $Ctx.Web.lists.GetByTitle($ListName)

#Add New Item to Link list
If($List -ne $Null)
{
    $ListItemInfo = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation
    $ListItem = $List.AddItem($ListItemInfo)
    $ListItem["Title"] = "Support Center"

    #Set Hyperlink field properties    
    $Link = New-Object Microsoft.SharePoint.Client.FieldUrlValue
    $Link.Url = "https://Crescent.sharepoint.com/sites/support"
    $Link.Description = "Support Center URL"

    #Update Hyperlink Field
    $ListItem[$FieldName] = [Microsoft.SharePoint.Client.FieldUrlValue]$Link
    $ListItem.Update()
    $Ctx.ExecuteQuery()
 
    Write-host "New Item Added to Link List!" -ForegroundColor Green
}

ค้นหาและแทนที่ไฮเปอร์ลิงก์โดยใช้ PowerShell:

ในทำนองเดียวกัน เราสามารถอัปเดตค่าฟิลด์ไฮเปอร์ลิงก์ที่มีอยู่ได้โดยใช้ PowerShell ใน SharePoint Online มาอัปเดตรายการลิงก์ทั้งหมดด้วย URL “/sites/support” ด้วย “/sites/supportV2”


#Load SharePoint Online 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"

#Variables
$SiteURL="https://crescent.sharepoint.com"
$ListName = "Quick Links"
$FieldName = "URL"

#Hyperlink Field Values
$HyperLinkValue = "Support Center"
$HyperLinkURL = "/sites/support" #Provide FULL URL for External URLs
$HyperLinkNewURL = "/sites/supportV2"

#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
$List = $Ctx.Web.lists.GetByTitle($ListName)

#Define the CAML Query to Filter by Hyperlink value and Get List Item
$Query = New-Object Microsoft.SharePoint.Client.CamlQuery
$Query.ViewXml = "@
<View>
    <Query>
        <Where>
            <Eq >
                <FieldRef Name='$FieldName' /><Value Type='URL'>$HyperLinkURL</Value>
            </Contains >
        </Eq>
    </Query>
</View>"
 
#Get All List Items matching the query
$ListItems = $List.GetItems($Query)
$Ctx.Load($ListItems)
$Ctx.ExecuteQuery()

#Update the List Item's Hyperlink Field value
If($ListItems.Count -gt 0)
{
    ForEach($Item in $ListItems)
    {
        #Get Hyperlink field value and update
        $HyperLinkField = [Microsoft.SharePoint.Client.FieldUrlValue]$Item[$FieldName]
        $HyperLinkField.Url = $HyperLinkNewURL
        $HyperLinkField.Description = $HyperlinkDescription

        #Update Hyperlink Field
        $Item[$FieldName] = $HyperLinkField
        $Item.Update()
        $Ctx.ExecuteQuery()
 
        Write-host "Hyperlink Field Value Updated in Link List!" -ForegroundColor Green
    }
}
Else
{
    Write-host "Hyperlink Field Value '$($HyperLinkURL)' Not Found in Link List!" -ForegroundColor Yellow
}

PnP PowerShell เพื่ออัปเดตคอลัมน์ไฮเปอร์ลิงก์ใน SharePoint Online:

หากต้องการตั้งค่าฟิลด์ไฮเปอร์ลิงก์หรือรูปภาพโดยใช้ PnP PowerShell cmdlet Set-PnPListItem ให้ใช้: รูปแบบ @{“Hyperlink”=“https://URL, LinkText”}


#Config Variables
$SiteURL = "https://Crescent.sharepoint.com/sites/marketing"
$ListName = "Projects"
$ItemID = 1

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

#Update Hyperlink column value using PowerShell
Set-PnPListItem -List $ListName -Identity $ItemID -Values @{"CompanyWebsite"= "https://www.CrescentTech.com, Crescent Technologies Inc."}

สคริปต์นี้เชื่อมต่อกับ SharePoint Online โดยใช้ PnP PowerShell และอัปเดตคอลัมน์ไฮเปอร์ลิงก์ “CompanyWebsite” ในรายการที่มี ID “1”

ค้นหาและแทนที่ลิงก์ในรายการ SharePoint Online โดยใช้ PnP PowerShell

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


#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Marketing"
$ListName = "Projects"
$ColumnName = "Project_x0020_Reference" #Internal Name of the Field
$OldString = "horizon" #Set it in lower case
$NewString = "crescent"

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

#Get All List Items
$ListItems = Get-PnPListItem -List $ListName -PageSize 2000

#Iterate through all items in the list
ForEach ($Item in $ListItems)
{
    $Flag =  $false
    $URL = $Item.FieldValues[$ColumnName].URL.ToLower()

    #Check if the URL has the old text
    If($URL.contains($OldString))
    {
        $URL = $URL -Replace $OldString,$NewString
        $Flag =  $True
    }

    #check the description of the Hyperlink
    $Description = $Item.FieldValues[$ColumnName].Description.ToLower()
    If($Description.contains($OldString))
    {
        $Description = $Description -Replace $OldString,$NewString
        $Flag =  $True
    }

    #update the Hyperlink field if URL or Description has the old string
    If($Flag -eq $True)
    {
        #Frame the URL field value
        $Hyperlink = New-Object Microsoft.SharePoint.Client.FieldUrlValue
        $Hyperlink.Url= $URL
        $Hyperlink.Description= $Description

        #Update Hyperlink field value
        Set-PnPListItem -List $ListName -Identity $Item.Id -Values @{$ColumnName = [Microsoft.SharePoint.Client.FieldUrlValue]$Hyperlink} | Out-Null
        Write-host -f Green "List Item $($Item.ID) Updated!"
    }
}

ด้วยการทำตามขั้นตอนที่ระบุไว้ในคู่มือนี้ คุณจะได้รับลิงก์ที่คุณต้องการอัปเดต อัปเดต URL และบันทึกการเปลี่ยนแปลงได้อย่างรวดเร็วและง่ายดาย

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