One of our customers needed some help to build out navigation from a .csv file in their Microsoft Office SharePoint Server 2007 environment.

PowerShell seemed to be the most simple option for them; so my colleague Andy and I created the following script:

[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

$url = "http://intranet"

$sc = New-Object Microsoft.SharePoint.SPSite($url)
$csv = Import-Csv -Path "e:\siteNav.csv"

foreach ($s in $sc)
{
    $w = $sc.OpenWeb()
    $tn = $w.Navigation.TopNavigationBar
   
   
    if (!$w.Navigation.UseShared)
    {
      
       foreach ($line in $csv)
       {
            if ($line.parent -eq "") {
                $l = New-Object Microsoft.SharePoint.Navigation.SPNavigationNode($line.description,$line.link,"true");
                $tn.AddAsLast($l);
            }
           
            elseif ($line.parent -ne "") {
               
               $navBar = $tn.Navigation.TopNavigationBar
              
               foreach ($navItem in $navBar){
                    if ($navItem.Title -eq $line.parent)
                    {
                        $x = New-Object Microsoft.SharePoint.Navigation.SPNavigationNode($line.description,$line.link,"true");
                        $navItem.Children.AddAsLast($x);
                    }
               }

            }
       }
      
       $w.Update()
       $w.Dispose()
   
    }
    
    $s.Dispose()
}

 

The CSV file was structured with three columns (Description, Link and Parent):