首页 > 解决方案 > 使用 PowerShell 脚本修复 SharePoint 2013 列表中超链接列的默认 URL

问题描述

我需要使用 PowerShell 脚本修复 SharePoint 2013 列表中超链接列的默认 URL。我正在尝试以下 PowerShell 脚本并收到错误消息。

PowerShell 脚本:

$myweb= Get-SPWeb http://sharepointtest/sites/test/
$mylist = $myweb.Lists["Test list"]
$myFieldName = $mylist.Fields["hyperlink"]
$defaultValue=$myFieldName.DefaultValue
$myFieldName.DefaultValue = 'https://www.google.com/'
$myFieldName.Update()
Write-Host 'complete'

错误信息:

The property 'DefaultValue' cannot be found on this object. Verify that the
property exists and can be set.
At C:\Users\spsetup\Code\defaultvalue.ps1:5 char:1
+ $myFieldName.DefaultValue = 'https://www.google.com/'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyNotFound

You cannot call a method on a null-valued expression.
At C:\Users\spsetup\Code\defaultvalue.ps1:6 char:1
+ $myFieldName.Update()
+ ~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

标签: powershellsharepointsharepoint-2013sharepoint-designer

解决方案


尝试这个:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Configuration Variables
$SiteURL = "http://sharepointtest/sites/test/"
$ListName = "Test list"
$FieldName="hyperlink"

#Get the Web, List Objects
$web = Get-SPWeb $SiteURL
$List = $Web.Lists.TryGetList($ListName)

If($list)
{
    #sharepoint powershell update hyperlink field
    $Picture = New-Object Microsoft.SharePoint.SPFieldURLValue
    $Picture.Description = "hyperlink"
    $Picture.URL = "http://sharepointtest/sites/test/Images/profile.jpg"

    #Add new List Item
    $Item = $List.AddItem()
    $Item[$FieldName] = $Picture
    $Item.Update() 

    Write-host "New Item Added Successfully!"
}

推荐阅读