Tìm kiếm trang web

SharePoint Online: Cập nhật giá trị trường siêu kết nối bằng PowerShell


Yêu cầu: Cập nhật giá trị trường siêu liên kết trong SharePoint Online bằng PowerShell.

SharePoint Online PowerShell để cập nhật trường siêu kết nối

Các trường siêu liên kết có thể được sử dụng để liên kết các tài liệu, danh sách các mục, trang, v.v. Trong một số trường hợp, bạn có thể cần cập nhật URL siêu kết nối hoặc văn bản liên kết cho các mục hiện có trong danh sách SharePoint Online của mình. Bài đăng blog này sẽ chỉ cho bạn cách cập nhật giá trị trường siêu liên kết trong SharePoint bằng tập lệnh PowerShell.

Hãy thêm một mục liên kết mới vào danh sách Liên kết bằng 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
}

Tìm và thay thế siêu liên kết bằng PowerShell:

Tương tự, chúng ta có thể cập nhật các giá trị trường siêu kết nối hiện có bằng PowerShell trong SharePoint Online. Hãy cập nhật tất cả các mục danh sách liên kết có URL “/sites/support” bằng “/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 để cập nhật cột siêu liên kết trong SharePoint Online:

Để đặt giá trị trường Siêu liên kết hoặc Hình ảnh bằng lệnh ghép ngắn PnP PowerShell Set-PnPListItem, hãy sử dụng định dạng: @{“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."}

Tập lệnh này kết nối với SharePoint Online bằng PnP PowerShell và cập nhật cột siêu liên kết “Trang web công ty” trong một mục danh sách có ID “1”.

Tìm và thay thế liên kết trong danh sách SharePoint Online bằng PnP PowerShell

Làm cách nào để tìm và thay thế một chuỗi cụ thể trong URL hoặc Mô tả giá trị trường siêu liên kết cụ thể trong tất cả các mục danh sách bằng PnP PowerShell? Bạn có thể thường xuyên cần cập nhật các liên kết đến các tài nguyên hoặc địa điểm khi chúng phát triển và thay đổi. Tôi phải tìm và thay thế tất cả các phiên bản của “đường chân trời” giá trị cũ bằng giá trị mới “lưỡi liềm” trong Danh sách 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!"
    }
}

Bằng cách làm theo các bước được nêu trong hướng dẫn này, bạn có thể nhanh chóng và dễ dàng nhận được liên kết bạn muốn cập nhật, cập nhật URL và lưu các thay đổi.

Bài viết liên quan