Recently was asked to update the hyperlink field in a SharePoint list. The list had thousands of links pointing to a server we were moving, so the URL in the list needed to updated to reflect the new location. The only options I could think of, were to open SQL Server and do find/update against the content database(s). Well, Microsoft doesn’t like up playing with the SharePoint databases. So I cracked open PowerShell and went to town trying to figure this out.
#Add-PSSnapin Microsoft.SharePoint.PowerShell $siteUrl = "http://sharepoint/SiteCollection/SiteName" $webName = “SiteName” $listName = "Name of your list" $spSite = new-object Microsoft.SharePoint.SPSite($siteurl) $spWeb = $spSite.OpenWeb($webName) $spList = $spWeb.Lists[$listName] foreach($Item in $spList.Items ) { $ofldurl= new-object Microsoft.SharePoint.SPFieldUrlValue($Item["URL"]) $ofldurl.URL = $ofldurl.URL.Replace("Nacho", "Taco") $ofldurl.Description = $ofldurl.Description.Replace("Nacho", "Taco") $item["URL"] = $ofldurl $item.update() } $spWeb.Dispose()
What I’m doing here is replacing part of the URL string with a new word. The script is looking for the string Nacho and replacing it with Taco.
**** update ****
After migrating from SharePoint / WSS 2007 to SharePoint 2010, we started to notice some of our URLs were messed up. Web parts ( Page Viewer) and internal hyperlinks were pointing to an incorrect location.
example:
http://sharepoint/crmsp/customers/sitename/_layouts/1033/mngsubwebs.aspx?view=sites
Notice the 1033, no bueno!
Using the script above, i was able to update the links.
#Add-PSSnapin Microsoft.SharePoint.PowerShell $siteUrl = "http://sharepoint/SiteCollection/SiteName" $webName = “SiteName” $listName = "Name of your list" $spSite = new-object Microsoft.SharePoint.SPSite($siteurl) $spWeb = $spSite.OpenWeb($webName) $spList = $spWeb.Lists[$listName] foreach($Item in $spList.Items ) { $ofldurl= new-object Microsoft.SharePoint.SPFieldUrlValue($Item["URL"]) $ofldurl.URL = $ofldurl.URL.Replace("1033/", "") $ofldurl.Description = $ofldurl.Description.Replace($ofldurl.Description, $ofldurl.Description) $item["URL"] = $ofldurl $item.update() } $spWeb.Dispose()
In SharePoint 2016, the Description property appears broken?
This is the only way I could manage to set the URL and display text:
$newItem[“LinkField”] = "http://taco.com, taco2"
When I tried to run the script (having first modified it to point to things in my world), I received the following error:
New-Object : Cannot find type [Microsoft.SharePoint.SPSite]: make sure the assembly containing this type is loaded.
At Z:\extranets\fixurls.ps1:6 char:21
+ $spSite = new-object <<<< Microsoft.SharePoint.SPSite($siteurl)
+ CategoryInfo : InvalidType: (:) [New-Object], PSArgumentException
+ FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand
It seems I missing some software, any ideas on what? Thanks!
I use the free PowerShell editor from Quest.
http://www.quest.com/powershell/
Make sure to add the add-pssnapin…
Also try updating a sub-site.
Your code should look something like this:
Hi,
We are getting the error “Value can not be null” at $item[“URL”] = $ofldurl.
Any idea?
What version of SP are you running?
$ofldurl= new-object Microsoft.SharePoint.SPFieldUrlValue($Item[“URL”])
URL is the name of a field in the List.
I get the same error as above… I can output the value of $ofldurl, and it is not NULL.
Running PS 4.
Was anyone able to get past this?
@brian
In SharePoint 2016, something must have changed. After messing with it for a bit, this is the only thing I could get to work:
$newItem[“LinkField”] = “http://taco.com”
$newItem.Update()
Setting the description property would cause the update to fail.
Hi,
I am using SharePoint 2013 and the only way I could get it work is by converting the urlfield to string, please give a try this code:
write-Host “deshabilitando eventos del workflow…”
$prop.SetValue($null, $true, $null); #Disable event firing
$tareaDerivada = $TareasDerivadasList.Items.GetItemById($itemAddedID);
#Prepare the Hyperlink column for asignar tarea
$linkAsignarTareaURL = $WebURL + “/Lists/derivacion/NewForm.aspx?IDInstructivoDerivacionPadre=” + $instructivoID + “|” + $itemAddedID
$linkAsignarTarea = New-Object Microsoft.SharePoint.SPFieldURLValue
$linkAsignarTarea.Description = “asignar tarea…”
$linkAsignarTarea.URL = $linkAsignarTareaURL
#Prepare the Hyperlink column for comentario
$linkComentarioURL = $WebURL + “/SitePages/comentario.aspx?IDDerivacion=” + $itemAddedID
$linkComentario = New-Object Microsoft.SharePoint.SPFieldURLValue
$linkComentario.Description = “ver comentarios…”
$linkComentario.URL = $linkComentarioURL
$tareaDerivada[“linkComentario”] = $linkComentario.ToString()
$tareaDerivada[“linkDerivar”] = $linkAsignarTarea.ToString()
$tareaDerivada.Update();
write-Host “habilitando eventos del workflow…”
$prop.SetValue($null, $false, $null); #Disable event firing
if you are encountering the null value issue “Value can not be null”
based on the above example
$ofldurl= new-object Microsoft.SharePoint.SPFieldUrlValue($Item[“URL”])
$ofldurl.URL = $ofldurl.URL.Replace(“1033/”, “”)
$ofldurl.Description = $ofldurl.Description.Replace($ofldurl.Description, $ofldurl.Description)
$item[“URL”] = $ofldurl
$item[“URL”] = [Microsoft.SharePoint.SPFieldUrlValue]$ofldurl
What version of SP are yo working with? I recall having issues with SP16 and SPO.
Can we update the URL’s inside a document(.docx or .doc)
The real short answer is yes. You would need process the data in memory or do something longer like download the file, process it, then replace it in SP. This post on stackexchange should guide you in the right direction: https://sharepoint.stackexchange.com/questions/188007/take-infopath-form-data-in-xml-and-upload-it-into-other-document-library
Thanks Jayaraja, I was struggling with Value cannot be null issue. Spent so many hours to resolve this issue. Your comment helped me resolved this issue.
$link.URL = $link.Url.Replace($link.Url, $UpdatedStrUrl)
if($link.Description -match $TextToFind)
{
$strDesc= $link.Description
$UpdatedStrDesc= $strDesc.ToString().Replace($TextToFind,”VUE+InSphere+Model”)
$link.Description = $link.Description.Replace($link.Description, $UpdatedStrDesc)
Write-Host $UpdatedStrDesc
}
$item[$FieldName] = [Microsoft.SharePoint.SPFieldUrlValue]$link
$item.Update()