当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 在 SharePoint 中的超链接列中查找并替换 URL 链接

作者:精品下载站 日期:2024-12-14 20:59:24 浏览:12 分类:玩电脑

在 SharePoint 中的超链接列中查找并替换 URL 链接


要求:
我们有一个名为“Crescent Portal”的 SharePoint 门户网站,其 URL:https://portal.crescent.com。从 SharePoint 2010 迁移到 SharePoint 2013 后,我们决定将网站名称和 URL 替换为“Crescent Intranet”,并将 URL 替换为“https://intranet.crescent.com”。

我们知道有大量列表和库在其超链接列中使用旧站点 URL 进行硬编码。我们必须从所有 SharePoint 列表和库中查找并替换那些旧链接。

用于查找和替换 SharePoint 超链接列中的链接的 PowerShell 脚本:


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

Function Replace-LinkFields($WebURL, $OldLinkURL, $OldLinkTitle, $NewLinkURL, $NewLinkTitle)
{
    #Get the Web
    $Web = Get-SPWeb $WebURL

    #Get all lists - Exclude System lists
    $ListCollection = $web.lists | Where-Object  { ($_.hidden -eq $false) -and ($_.IsSiteAssetsLibrary -eq $false) -and ($_.Author.LoginName -ne "SHAREPOINT\system") }
 
    #Iterate through each list
    foreach ($List in $ListCollection)
    {
        #find all HyperLink fields in list
        $HyperlinkFields = @()
        foreach ($field in $list.Fields) 
        {
            if ($field.TypeAsString -eq "URL") 
            {
                $HyperlinkFields = $HyperlinkFields + $field.Title      
            }
        }    
        write-host "Processing list at: $($web.url)/$($list.RootFolder.Url)"

        #Process all hyperlink fields found
        
        #Proceed with next list if Hyperlink field is not found in the list
        if($HyperlinkFields.Count -eq 0) { continue }

        foreach ($Item in $List.Items) 
        {
            #Iterate through HyperLink fields
            foreach ($field in $HyperlinkFields) 
            {
                #Get field value
                $FieldValue = $item[$field]
                #Skip nulls
                if($FieldValue -ne $null) 
                { 
                    # Check for OldLinkURL or OldLinkTitle
                    if( ($FieldValue.contains($OldLinkURL)) -or ($FieldValue.contains($OldLinkTitle)) )
                    {             
                        #Replace the OLD URL with New URL
                        $Item[$field] = ($item[$field] -Replace $OldLinkURL,$NewLinkURL) 
                        #Replace OLD link title with new link title
                        $Item[$field] = ($item[$field] -Replace $OldLinkTitle,$NewLinkTitle) 

                        $Item.update()
                        Write-host "Found and replaced a old link item at: $($web.Url)/$($list.RootFolder.Url) - Item id: $($item.id)"      
                    }
                }
            }
        }
    }   
}

#Call the function to Replace Links in lists
Replace-LinkFields "https://Intranet.crescent.com/" "https://portal.crescent.com" "Crescent Portal" "https://intranet.crescent.com" "Crescent Intranet"

该脚本扫描给定站点的所有列表和库并替换旧链接。您可以更改处理 Web 应用程序中所有站点的逻辑,以替换 Web 应用程序中的旧链接。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯