locked
SP2013 Powershell find URL value in list and update it RRS feed

  • Question

  • I have a List in SharePoint 2013 (On prem).

    It contains
    Site Name - Single line of text
    Primary Admin - Person or Group
    Secondary Admin - Person or Group
    Site Link - Hyperlink or Picture

    The site link contains two parts separated by a comma. The first part is the Address: (e.g. URL) and the second part is the text to display. (Many of the items in the second part have been entered as a URL instead of a name and I need to change all of these)

    I need a powershell script which will find all items where the Second part of "Site Link" starts with "http:"
    Then replaced the content of only the second part with the "Site Name" value for that entry


    • Edited by GTEM Thursday, September 27, 2018 8:04 PM typo
    Thursday, September 27, 2018 8:03 PM

Answers

  • Okay. Firstly thank you for both responses.
    I was probably already at the point of the first response when I posted the question.
    The second response was not quite what thought it was (thank goodness I did not use it).
    The link provided took me to the site where the code was copied from for a different problem. 
    The response on that site did however have a link to yet another site, that details most of the steps on how to do what I want to do.
    I had to work my way slowly through each step on that site to slowly put together the steps that I need.
    This is what I ended up with.

    Note: I decided not to let it loop through all items in the list to do them all in one go. Instead running it in PowerShell ISE by continually hitting F5 until all was complete, which allowed me to double check and stop if needed.

    <# Run the next 4 lines in ISE beforehand 
    Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
    $c = 0
    $w = Get-SPWeb "http://contoso.com/subsite1/subsite2"
    $l = $w.GetList("directories/subsite2/Lists/List Name");
    #>
    
    # Restart here 
    $X = $l.Items.Count - $c - 1
    write-host "Doing $c, $X remaining of $($l.Items.Count)"
    $i = $l.Items[$c];
    #$i | Select Title
    $i["Title"]
    $i["Service area"]
    
    $h = $i["Site Name and link"] -as [Microsoft.SharePoint.SPFieldUrlValue];
    $h.Url
    $h.Description
    
    if($h.Description.StartsWith("http")){
        Write-Host "Yes"
        $h.Description = $i["Title"]
        $i["Site Name and link"] = $h
        $i.Update()
        $c = $c + 1
    } else {
        Write-Host "No"
        $c = $c + 1
    }


    Monday, October 1, 2018 1:28 PM

All replies

  • This may be handy when trying to find specific items in a list based on values of various fields:

    $web = Get-SPWeb http://yourweb
    $list = $web.Lists["Your Library Name"]
    
    // this is the bit - get items of a particular content type
    // ? is shorthand for where, and $_ is the item in the pipeline
    $listItems = $list.Items | ?{$_.ContentType.Name -eq "Content Type Name"}
    
    // or items based on a custom column - if using -like then the wildcard is *
    $listItems = $list.Items | ?{$_["InternalFieldName"] -like "*this*"
    
    // you could join them up using -and
    $listItems = $list.Items | ?{$_.ContentType.Name -eq "Content Type Name" -and $_["InternalFieldName"] -like "*this*"
    
    // or iterate the loop and print them out
    foreach($item in $listItems) { Write-Host $item.Name, $item["InternalFieldName"] }
    
    or more directly
    
    $list.Items | ?{$_.ContentType.Name -eq "Content Type Name" -and $_["InternalFieldName"] -like "*this*" | foreach { $_.Name, $_["InternalFieldName"]
    
    // or count them
    $listItems.Count
    
    or
    
    $list.Items | ?{$_.ContentType.Name -eq "Content Type Name" -and $_["InternalFieldName"] -like "*this*" | foreach {$count++}
    $count


    Friday, September 28, 2018 1:57 AM
  • Hi GTEM,

    There is a demo for your reference:

    $w= Get-SPWeb -Identity " your site" ; $l = $w.GetList("your list"); foreach($i in $l.Items) { $hyperlink = $i["hylink"] -as [Microsoft.SharePoint.SPFieldUrlValue]; $url=$hyperlink.Url; #$newurl =$url -replace ",SDAT"," "

    $newDescription="string"; $hyperlinkfield = $l.Fields["hylink"] -as [Microsoft.SharePoint.SPFieldUrl]; $urlFieldValue = New-Object Microsoft.SharePoint.SPFieldUrlValue; $urlFieldValue.Description = $newDescription; $urlFieldValue.Url = $url; $hyperlinkfield.ValidateParseAndSetValue($i,$urlFieldValue.ToString()); #SharePoint 2013 only $i.Update(); $hyperlinkfield.ParseAndSetValue($i,$urlFieldValue.ToString()); #SharePoint 2010 and 2013 $i.Update(); }

    More information, please refer to the thread:

    https://social.technet.microsoft.com/Forums/ie/en-US/b3e78fcb-00e6-4988-b06e-a93e5993c4d8/bulk-update-url-column-in-a-sharepoint-2013-list?forum=sharepointgeneral

    Thanks,

    Wendy


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Friday, September 28, 2018 10:20 AM
  • Okay. Firstly thank you for both responses.
    I was probably already at the point of the first response when I posted the question.
    The second response was not quite what thought it was (thank goodness I did not use it).
    The link provided took me to the site where the code was copied from for a different problem. 
    The response on that site did however have a link to yet another site, that details most of the steps on how to do what I want to do.
    I had to work my way slowly through each step on that site to slowly put together the steps that I need.
    This is what I ended up with.

    Note: I decided not to let it loop through all items in the list to do them all in one go. Instead running it in PowerShell ISE by continually hitting F5 until all was complete, which allowed me to double check and stop if needed.

    <# Run the next 4 lines in ISE beforehand 
    Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
    $c = 0
    $w = Get-SPWeb "http://contoso.com/subsite1/subsite2"
    $l = $w.GetList("directories/subsite2/Lists/List Name");
    #>
    
    # Restart here 
    $X = $l.Items.Count - $c - 1
    write-host "Doing $c, $X remaining of $($l.Items.Count)"
    $i = $l.Items[$c];
    #$i | Select Title
    $i["Title"]
    $i["Service area"]
    
    $h = $i["Site Name and link"] -as [Microsoft.SharePoint.SPFieldUrlValue];
    $h.Url
    $h.Description
    
    if($h.Description.StartsWith("http")){
        Write-Host "Yes"
        $h.Description = $i["Title"]
        $i["Site Name and link"] = $h
        $i.Update()
        $c = $c + 1
    } else {
        Write-Host "No"
        $c = $c + 1
    }


    Monday, October 1, 2018 1:28 PM
  • Glad this worked for you. But looks like you could simplify this code. If you're interested let me know. Other wise you should mark your answer as the answer

    Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
    
    $w = Get-SPWeb "http://contoso.com/subsite1/subsite2"
    $l = $w.lists["List Name"]
    $items = $l.items
    
    Foreach($item in $items)
    {
    
    ###code here
    
    }


    If this is helpful please mark it so. Also if this solved your problem mark as answer.


    • Edited by taylor.l Monday, October 1, 2018 3:17 PM
    Monday, October 1, 2018 3:13 PM