Wednesday, February 29, 2012

Powershell Script to Customize Sharepoint default Left Navigation

We can use the following script to delete/add navigation urls to the sharepoint default left navigation. Following script deletes the existing navigation nodes and add according to the given structure in the xml file.

Powershell script:

[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
function New-SPNavigation{
[CmdletBinding()]
                Param(   
                [Parameter(Mandatory=$false)]
                [switch]$CreateFromXml      
    )
# Create From Xml
if ($CreateFromXml) {
    #Get XML File
    $xmlFilePath = "C:\Projects\BuildScripts\LeftNavigation.xml"
    $xmlFile = [xml](Get-Content($xmlFilePath))
    Write-Host $xmlFile.Navigation.WebUrl

    #Get Web and Quick Launch objects
    $siteUr = $xmlFile.Navigation.WebUrl

    $site = New-Object Microsoft.SharePoint.SPSite($siteUr)
    $web = $site.OpenWeb()
    $currentLinks = @()
    $qlNav = $web.Navigation.QuickLaunch  

    #Clear Quick Launch links
    $qlNav | ForEach-Object {
        $currentLinks = $currentLinks + $_.Id     
    }  

    $currentLinks | ForEach-Object {
        $currentNode = $web.Navigation.GetNodeById($_)    
        write-host "Deleting" $currentNode.Title "and all child navigation links..."
        $qlLibraries = $currentNode.Children   
        $qlNav.Delete($currentNode)
    }

    #Create Quick Launch Links
    $xmlFile.Navigation.Headings.Heading | ForEach-Object {
        $headingNode = New-Object Microsoft.SharePoint.Navigation.SPNavigationNode($_.Title, $_.Url, $true)
        write-host "Creating Heading:" $_.Title
        $heading = $qlNav.AddAsLast($headingNode)
        $a=$_.NavLink.Count
        if($a -gt 0)
        {
            $_.NavLink | ForEach-Object {
                $linkNode = New-Object Microsoft.SharePoint.Navigation.SPNavigationNode($_.Title, $_.Url, $true)
                write-host "Creating Navigation Link:" $_.Title
                $link = $heading.Children.AddAsLast($linkNode)
            }
        }     
        $web.Update()
    }#End ForEach Hedding 

    $web.Dispose()
    $site.Dispose()
  }
 

Add-PSSnapin "Microsoft.SharePoint.PowerShell" -EA "SilentlyContinue"
new-spnavigation -createfromxml 
Write-Host "Press any key to continue ..."
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") 

XML File: 

<?xml version="1.0" encoding="utf-8" ?>
  <Navigation>
  <WebUrl>http://<site url></WebUrl> 
    <Headings>
      <Heading Title='My Profile' Url='/test'></Heading>
      <Heading Title='My Place' Url='/test/'>
        <NavLink Title='Home' Url='/test/Pages/Home.aspx'></NavLink>
        <NavLink Title='Google Map' Url='/test/Pages/GoogleMap.aspx'></NavLink>
      </Heading>
      <Heading Title='My Links' Url='/test'>
        <NavLink Title='Yahoo' Url='http://www.yahoo.com'></NavLink>
        <NavLink Title='Facebook' Url='http://www.facebook.com'></NavLink>
         <NavLink Title='Google' Url='http://www.google.com'></NavLink>
      </Heading>  
    </Headings>
  </Navigation>

References:

1 comment: