March 29, 2016

SharePoint Left Navigation Using PowerShell

Hi,
 
I am working on PowerShell scripts to automate deployment for one of client,
 
Here is post :: Creating SharePoint Left Navigation using PowerShell,

 
First step will be creating XML File as below,
 
<?xml version="1.0" encoding="utf-8"?>
<Navigation>
  <Headings>
    <Heading Title="Heading1" Url="" Description=""  Permission="domain\UserGroup">
      <NavLink Title="HeadNav1" Url"/>
      <NavLink Title="HeadNav2" Url=""/> 
    </Heading>
    <Heading Title="Heading2" Url="" Description="" Permission="domain\UserGroup" >
      <NavLink Title="HeadNav3" Url=""/>
      <NavLink Title="HeadNav4" Url=""/>
    </Heading>
    <Heading Title="Heading3" Url="" Description="" Permission="domain\UserGroup" >
    </Heading>
  </Headings>
</Navigation>
 

Save the xml file, now time for PowerShell script to read the xml and build the left navigation.
 
PowerShell script will first clear the existing Left Navigation and then Create New left Navigation with
 
  1. Heading
  2. NavLink Under Heading
  3. Description for Heading
  4. URL link for Heading and NavLink
  5. Permission (Target Audience) for Heading

# Add SharePoint Snapin to PowerShell          
if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null) {          
  Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}

function LeftNavBar{
 
    $qlNav = $OpenWeb.Navigation.QuickLaunch
 
    #Get XML File
    $xmlFilePath = "$PSScriptRoot\.xml"
    $xmlFile = [xml](Get-Content($xmlFilePath))
    $oldWebUrl = $OpenWeb.Url
    $currentLinks = @()
 
    #Clear Quick Launch links
    $qlNav | ForEach-Object {
        $currentLinks = $currentLinks + $_.Id
    }
    $currentLinks | ForEach-Object {
        $currentNode = $OpenWeb.Navigation.GetNodeById($_)
        write-host -f Yellow "Deleting" $currentNode.Title "and all child navigation links..."
        $qlNav.Delete($currentNode)
    }
 
    #Create Quick Launch Links
    $xmlFile.Navigation.Headings.Heading | ForEach-Object {
        $headingNode = New-Object Microsoft.SharePoint.Navigation.SPNavigationNode($_.Title, $_.Url)
        #$headingNode.Properties["Description"] = $_.Description
        #$headingNode.Update();
        write-host -f Green "Creating Heading:" $_.Title
        $heading = $qlNav.AddAsLast($headingNode)
        $heading.Properties["Audience"] = $_.Permission
        $heading.Update();
      
        $_.NavLink | ForEach-Object {
            if($_.Title -ne $null) {
            $linkNode = New-Object Microsoft.SharePoint.Navigation.SPNavigationNode($_.Title, $_.Url)
            write-host -f Green "Creating Navigation Link:" $_.Title
            $link = $heading.Children.AddAsLast($linkNode)
            }
        }
    }
  
 
    $OpenWeb.Dispose()
}


$SiteURL = Read-Host -Prompt 'Enter Root Site URL and / @ end'
 
$OpenWeb = Get-SPWeb $SiteURL

LeftNavBar

No comments: