Thursday, July 5, 2012

Using Powershell to mass update a SharePoint Hyperlink field


Scenario:
You have a SharePoint List that contains all of your site's subsites, one of the columns in the List is used to point to the URL of the subsite. Using a foreach loop and comparing values in the List to the name of the sub site you can update the hyperlink column like so:

#add-pssnapin microsoft.sharepoint.powershell
$web = get-spweb -identity "https://dashboard.company/.com"
# We have to use the @ sign specify that this will be an array
$subsites = @($web.webs)
foreach($subsite in $subsites)
       {
        # The hyperlink field has 2 columns the URL and the Description
        $itemUrl = $subsite.Url
        $itemDesc = $subsite.Title
        #We filter the list to make sure we do not get
        # any items named Temp
        if($itemDesc -ne "temp")
        {
        # Get the List we want to update
        $listWeb = Get-SPWeb -identity "https://dashboard.company.com/orders"
        $list = $listWeb.Lists["Your List Name"]
        # We have to use the @ sign specify that this will be an array
        $items = @($list.Items)
        foreach($item in $items)
        {
            $siteUrl = $item["Your Column Name"]
            $to = $item["Task Order"]
            # We want to match up each List item with the right Title of each site
            if($itemDesc -eq $to)
            {
            # Set the name of the hyperlink column in a variable
            $siteUrl = $item["Your Column Name"]
            # You have to enclose the variables in quotes for the vaules
            # to be passed
            $siteUrl = "$itemUrl, $itemDesc"
            $item["Your Column Name"] = $siteUrl
            $item.update()
            # Print out the sub site name to the console window
            # goes faster if you do not print it out           
            $siteUrl
            $listWeb.Dispose()
            }
          }
        }
    }
$web.Dispose()

No comments: