SharePoint PowerShell Script Series Part 7: Updating or Viewing the Quick Launch Bar
SharePoint Administrator’s running this script want to perform the following tasks for a specified site:
1. Add new Headings and Links to the Quick Launch Bar
2. Add new links to existing Headings on the Quick Launch Bar
3. View the Quick Launch layout for a given Site
Question: What is the quick launch bar?
Answer: The quick launch bar resides on the left hand side of a SharePoint site and consists of Headings and Links. It’s an easy way for users to quickly navigate to internal SharePoint data like other sites, lists, or document libraries. It’s also an easy way for users to navigate to external SharePoint content like an external web site.
Quick Launch bar is circled in red:
It’s possible to update the quick launch bar by going to Site Actions\Site Settings and under Look and Feel section, click on Navigation link. If you don’t have a navigation link, you’ll need to enable the SharePoint Server Publishing Infrastructure feature which is also under Site Actions\Sites Settings and under Site Collection Administration, click on Site Collection Features. After clicking on Navigation link, you’re taken to the areanavigationsettings.aspx page. Within this page, you have a nice navigation control to Add, Delete, or Move Headings and/or Links.
Note: Everything under the Current Navigation reflects the Quick Launch bar layout.
Question: Do sub-sites automatically inherit the current navigation “quick launch bar” from the root site?
Answer: No, by default when creating a sub-site using team site template, navigation isn’t inherited from the root.
Question: Can I control what whether or not a subsite inherits the quick launch bar from the parent site?
Answer: Yes, in fact from the navigation page I mentioned above is the following section:
Note: The picture above has the default setting selected which is not to inherit from the parent.
Intro into the Quick Launch PowerShell script
First, I really don’t know why I wrote this script other than being curious on how to do it. I wanted to use PowerShell to add Headings and Links to the quick launch bar in an automated fashion. I also wanted the ability to view the quick launch bar.
Before running this PowerShell Script, a couple of important things to point out:
1. If you add headings and/or links to a parent site, any subsite inheriting current navigation from the parent will also see the newly added headings and/or links.
2. If you choose to add headings and/or links to a subsite, the script will only add to subsites that have current navigation set to the following:
Display only the navigation items below the current site
Scenario 1: Add new heading and links to parent site
In this scenario, I’m adding a new heading with 2 links in the parent site or root web in the Site Collection. The heading will be external and will contain two external links to MSDN and Technet.
Setup looks like:
External
Heading Name: MSFT Resources
Link Name: MSDN
Link URL: https://msdn.microsoft.com
Link Name: TechNet
Link URL: https://technet.microsoft.com
1. Prompt to run against root site or subsite and I choose the root:
2. Prompted to enter the URL and hit enter
3. Prompted to choose from 3 options. I chose option 1.
4. Prompted to choose external or internal links. I chose External
5. Prompted to type in name of new heading and press enter.
6. Prompted to type in # of links to add.
7. Prompted to enter name and URL for each link.
8. Get response Script is completed:
9. Final result:
Scenario 2: Add new two links to a sub site
In this scenario, I’m adding 2 internal links to an existing heading called Departments that resides in a subsite. The heading is internal and I’ll be adding to internal links that point to two lists that reside in the subsite.
Setup looks like:
Internal
Existing Heading Name: Departments
Link Name: Sales
Link URL: /subsite/lists/sales/allitems.aspx
Link Name: IT
Link URL: /subsite/lists/IT/allitems.aspx
1. Prompt to run against root site or subsite and I choose the subsite.
2. Prompted to enter the URL and hit enter
3. Next, I choose option 2 to add new links to existing heading.
4. Next, I choose 1 for Internal links
5. Next, I choose Option 2 to create links directly under a heading:
6. Prompted to choose existing heading to create links under. I chose 4:
7. Now I choose the # of links, link names, and internal relative URL to both lists:
8. Script is complete!
9. Final Result:
Scenario 3: Viewing Quick Launch bar of parent site
In this scenario, I’m viewing the Quick Launch bar of parent site by running the script. Currently, the parent site displays the following quick launch bar:
Most of the steps are identical except I choose option 3 to view quick launch bar:
The final output provides the Header and associated links:
Note: This PowerShell script is tested only on SharePoint 2010
Instructions for running the script:
1. Copy the below script and save it in notepad
2. Save it with a anyfilename.ps1 extension
3. To run, copy the file to a SharePoint Server
4. Select Start\Microsoft SharePoint 2010 Products\SharePoint 2010 Management Shell
5. Browse to directory holding the copied script file
6. Run the script: .\anyfilename.ps1 (assuming anyfilename is the name of the file)
<# ==============================================================
//
// Microsoft provides programming examples for illustration only,
// without warranty either expressed or implied, including, but not
// limited to, the implied warranties of merchantability and/or
// fitness for a particular purpose.
//
// This sample assumes that you are familiar with the programming
// language being demonstrated and the tools used to create and debug
// procedures. Microsoft support professionals can help explain the
// functionality of a particular procedure, but they will not modify
// these examples to provide added functionality or construct
// procedures to meet your specific needs. If you have limited
// programming experience, you may want to contact a Microsoft
// Certified Partner or the Microsoft fee-based consulting line at
// (800) 936-5200.
//
// For more information about Microsoft Certified Partners, please
// visit the following Microsoft Web site:
// https://partner.microsoft.com/global/30000104
//
// Author: Russ Maxwell (russmax@microsoft.com)
//
// ---------------------------------------------------------- #>
[Void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
Start-SPAssignment -Global
function question1()
{
Write-Host "Option 1: Quick Launch: Add new heading with link\links"
Write-Host "Option 2: Quick Launch: Add new link\links to existing heading"
Write-Host "Option 3: Quick Launch: View all Quick Launch Bar headings/links for specified Site"
$res2 = Read-Host "Press 1,2 or 3 and hit Enter"
return $res2
}
function question2()
{
##More Questions##
Write-Host "Will Links point to internal SharePoint data or External data outside of SharePoint?"
Write-Host "Option 1: Internal"
Write-Host "Option 2: External"
$res3 = Read-Host "Select 1 or 2 and press Enter"
return $res3
}
function addQuickLaunch()
{
##If True, Adding Heading and Links##
if($res2 -eq 1)
{
$empstr = ""
if($res3 -eq 1)
{
$navheadnode = New-Object Microsoft.SharePoint.Navigation.SPNavigationNode($headstr, $empstr, $false)
Write-Host
Write-Host
}
elseif($res3 -eq 2)
{
$navheadnode = New-Object Microsoft.SharePoint.Navigation.SPNavigationNode($headstr, $empstr, $true)
}
$navquicklaunch.addaslast($navheadnode)
$navheadnode.properties["BlankUrl"] = $true
$navheadnode.properties["NodeType"] = "Heading"
$navheadnode.update()
Write-Host
Write-Host
[Microsoft.SharePoint.Navigation.SPNavigationNodeCollection] $childcoll = $navheadnode.children
addLinks
}
##If True, Adding links under an existing heading##
elseif($res2 -eq 2)
{
Write-Host "Option 1: Create links directly under the Quick Launch bar"
Write-Host "Option 2: Create links directly under a heading"
$res4 = Read-Host "Press option 1 or 2 and hit Enter"
if($res4 -eq 1)
{
[Microsoft.SharePoint.Navigation.SPNavigationNodeCollection] $childcoll = $navquicklaunch.children
addlinks
}
elseif($res4 -eq 2)
{
Write-Host
Write-Host "Choose the heading to place links under"
[Microsoft.SharePoint.Navigation.SPNavigation] $nav2 = $web.Navigation
[Microsoft.SharePoint.Navigation.SPNavigationNodeCollection] $quickLaunch2 = $nav2.quicklaunch
$ctr1 = 1
foreach($node in $quickLaunch2)
{
Write-Host "Option "$ctr1 ". " $node.Title -ForegroundColor Magenta
$ctr1++
}
}
$res5 = Read-Host "Select the appropriate option and press enter"
if($res5 -ne $null)
{
$ctr1 = 1
foreach($node in $quickLaunch2)
{
if($res5 -eq $ctr1)
{
[Microsoft.SharePoint.Navigation.SPNavigationNodeCollection] $childcoll = $node.Children
addlinks
}
$ctr1++
}
}
}
}
###########################
##Function adds the links##
###########################
function addLinks
{
##Create arrays of names\links##
$linknamearr = @()
$linkurlarr = @()
Write-Host "Please type the # of links you would like to add"
$linknum = Read-Host
$ctr = 1
while($ctr -le $linknum)
{
$navlinkname = Read-Host "Enter Name for link: " $ctr "(Press Enter)"
$linknamearr += ,$navlinkname
if($res3 -eq 1)
{
$navlinkurl = Read-Host "Enter Internal relative URL to SharePoint Data and press enter"
$linkurlarr += ,$navlinkurl
}
elseif($res3 -eq 2)
{
$navlinkurl = Read-Host "Enter External URL for link and press Enter"
$linkurlarr += ,$navlinkurl
}
Write-Host
Write-Host
$ctr++
}
###Now iterate through arrays and add nodes###
$ctr = 0
while($ctr -lt $linknum)
{
if($res3 -eq 1)
{
$navlinknode = New-Object Microsoft.SharePoint.Navigation.SPNavigationNode($linknamearr[$ctr], $linkurlarr[$ctr], $false)
}
elseif($res3 -eq 2)
{
$navlinknode = New-Object Microsoft.SharePoint.Navigation.SPNavigationNode($linknamearr[$ctr], $linkurlarr[$ctr], $true)
}
$childcoll.addaslast($navlinknode)
$navlinknode.isVisible = $true
$navlinknode.Properties["vti_navsequencechild"] = $false
$navlinknode.Properties["NodeType"] = "AuthoredLinkPlain"
$navlinknode.update()
$ctr++
}
}
####################################
##Function to display quick launch##
####################################
function displayQuickLaunch()
{
[Microsoft.SharePoint.Navigation.SPNavigation] $nav = $web.Navigation
[Microsoft.SharePoint.Navigation.SPNavigationNodeCollection] $quickLaunch = $nav.quicklaunch
foreach($node in $quickLaunch)
{
Write-Host
Write-Host
Write-Host "Header: " $node.Title -ForegroundColor Cyan
$subnodeColl = $node.Children
foreach($subnode in $subnodeColl)
{
Write-Host "Link Name: " $subnode.title -foregroundcolor Magenta
Write-Host "Link URL: " $subnode.url -foregroundcolor Magenta
Write-Host
}
}
}
######################################
##Quick Launch Script Starts Here#####
######################################
Write-Host "Option 1: Modify or View QuickLaunch for root site in a given Site Collection"
Write-Host "Note: Making changes to the root of the Site Collection will push those changes to all subsites inheriting Navigation." -ForegroundColor Red
Write-Host
Write-Host "Option 2: Modify or View QuickLaunch for a subsite that isn't inheriting navigation from the root site"
Write-Host
$res1 = Read-Host "Press 1 or 2 and hit Enter"
Write-Host
if($res1 -eq 1)
{
Write-Host "Enter the URL of the Desired Site Collection and press Enter"
$siteURL = Read-Host
$site = Get-SPSite $siteURL
$web = $site.rootweb
if($web -ne $null)
{
$navobj = $web.Navigation
$navquicklaunch = $navobj.QuickLaunch
$res2 = question1
Write-Host
if($res2 -eq 1)
{
$res3 = question2
Write-Host
Write-Host "Please type the name of the new heading and press Enter"
$headstr = Read-Host
Write-Host
##Calling function which will add the heading/links##
addquickLaunch
Write-Host
Write-Host "Script is completed" -ForegroundColor Green
}
elseif($res2 -eq 2)
{
$res3 = question2
Write-Host
##Calling function which will add the heading/links##
addQuickLaunch
Write-Host
Write-Host "Script is completed" -ForegroundColor green
}
elseif($res2 -eq 3)
{
displayQuickLaunch
}
}
else
{
Write-Host
Write-Host
Write-Host "The Site is null or you entered an invalid URL" -ForegroundColor Red
Write-Host "Script completed making 0 changes" -ForegroundColor Green
}
}
elseif($res1 -eq 2)
{
Write-Host "Enter the url of the Subsite and hit Enter"
$subUrl = Read-Host
$web = Get-SPWeb $subUrl
$site = $web.site
if(($web -ne $null) -and ($web.url -ne $site.rootweb.url))
{
$res2 = question1
Write-Host
###Ensure navigation isn't inheriting from parent or isn't set to show siblings###
if(($web.allproperties["__InheritCurrentNavigation"] -eq "false") -and ($web.allproperties["__NavigationShowSiblings"] -eq "false"))
{
$navobj = $web.Navigation
$navquicklaunch = $navobj.QuickLaunch
if($res2 -eq 1)
{
$res3 = question2
Write-Host
Write-Host "Please type the name of the new heading and press Enter"
$headstr = Read-Host
addQuickLaunch
Write-Host
Write-Host
Write-Host "Script Complete!" -ForegroundColor Green
}
elseif($res2 -eq 2)
{
$res3 = question2
Write-Host
##Calling function which will add the heading/links##
addQuickLaunch
Write-Host
Write-Host "Script is completed" -ForegroundColor green
}
elseif($res2 -eq 3)
{
displayQuickLaunch
}
}
##Checking to see if inheriting navigation##
elseif($web.allproperties["__InheritCurrentNavigation"] -eq "true")
{
Write-Host "Navigation is inheriting from parent web!" -ForegroundColor Red
Write-Host "Script completed making 0 changes" -ForegroundColor Green
}
##Check to see if navigation is set to share siblings##
elseif($web.allProperties["__NavigationShowSiblings"] -eq "true")
{
Write-Host "Navigation isn't inheriting but is displaying site siblings" -ForegroundColor Red
Write-Host "Script completed making 0 changes" -ForegroundColor Green
}
}
else
{
Write-Host
Write-Host
Write-Host "The Site is null or is the Parent Web" -ForegroundColor Red
Write-Host "Script completed making 0 changes" -ForegroundColor Green
}
}
Stop-SPAssignment -Global
Thanks!
Russ Maxwell, MSFT