PowerShell - Change Order of Items in Quick Launch

Hello,

I recently added a link to the quick launch menu on some of my sites. The Quick Launch is structured with the same headings and links on each site. The new link is in the wrong place. I need to move the link up a few rows to maintain alphabetical order.  On the following script, I am getting You cannot call a method on a null-valued expression." What am I doing wrong? Or, how can I move my recently added link to a new location within the same heading?

$data = Import-Csv "C:\Scripts\Test\Test Sites.csv"
foreach ($d in $data)
{
$web = Get-SPWeb $d.SiteUrl
$qlNav = $web.Navigation.QuickLaunch
$qlHeading = $d.Header
$LinkHeader = $d.Header
foreach ($header in $web.Navigation.QuickLaunch)
{
if ($header.Title -eq $LinkHeader)
{
$qlNewPreviousSibling = $qlHeading.Children | where { $_.Title -eq "Agreements" }
$qlLink = $qlHeading.Children | where { $_.Title -eq "Property Contacts" }
$qlLink.Move($qlHeading.Children, $qlNewPreviousSibling)
}
}
}

I would also be open to setting up the link's order when I add it in the first place.

July 30th, 2015 5:48pm

Hi,

To change order of items in Quick Launch using PowerShell, the code demo below would be helpful:

$site = Get-SPSite "http://parkc/sites/quick"
$web = $site.RootWeb 

function ReOrder-QuickLaunch([array]$Navigations)
{
    $navLibaries = $web.Navigation.QuickLaunch | Where {$_.Title -eq "Head01"}

    $FirstOneInTheArry = $true
    foreach ($nav in $Navigations)
    {
        if ($FirstOneInTheArry -eq $true)
        {
            # define the first element as the first child under <Head01>
            $FirstOneInTheArry = $false
            $navChild =  $navLibaries.Children | Where {$_.Title -eq $nav}
            $navChild.MoveToFirst($navLibaries.Children)
        }
        else
        {
            # define the first element as the first child under <Head01>
            $navPreviousChild  =$navChild
            $navChild =  $navLibaries.Children | Where {$_.Title -eq $nav}
            $navChild.Move($navLibaries.Children, $navPreviousChild)
        }
    }
}

ReOrder-QuickLaunch @("child02","child01","child03")


The order of the links under the heading Head01 would be like this:

Thanks
Free Windows Admin Tool Kit Click here and download it now
July 31st, 2015 5:41am

Hello, thanks for your reply. I am trying to use the sample above with updates info for my site, heading and quick launch links. I am getting "you cannot call a method on a null-valued expression" for both $navChild.MoveToFirst($navLibaries.Children) and $navChild.Move($navLibaries.Children, $navPreviousChild).

In general I do not understand why I would need to movetofirst. A little more info about how this script works might help.


Thank you!


  • Edited by AnnaESP 11 hours 37 minutes ago
July 31st, 2015 10:46am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics