Sometimes doing a task 100 times through the UI just doesn’t seem right – this is one of those times. I had a need to add a heading and several links to every site in a Site Collection. I wanted the script to be easy enough to where you wouldn’t have to modify it for each site collection or site. The result of this effort became Set-SPGlobalNav – a function that thinks it’s a cmdlet!
Requirement
In order to do headings with drop down links, you must have SharePoint Server 2010 and the Publishing Feature activated.
Input File
The script requires a CSV file storing the Link Title and Link URL with the following format:
Load and View Help
Using PowerShell, navigate on your SharePoint server to where you have the saved script and perform the following to load the function:
Note that there is a space between the first and second periods. When you use a period like this you are telling PowerShell to load up all the variables, functions, and whatever else is in that script into its memory for your use.
The script was built to act like a cmdlet (command-let), so it provides help documentation as well as being able to pipe to it from Get-SPWeb. Below shows the help for Set-SPGlobalNav:
You can use “help Set-SPGlobalNav -examples” to see example uses of the script:
Call Script and Verify Results
For my given scenario, I want to add a new heading and various links to every site in the entire site collection. To perform this task I would use the below command:
Get-SPSite http://portal.company.com | Get-SPWeb -Limit All | Set-SPGlobalNav -Heading “New Heading” -Links “c:\solutions\links.csv”
Now, finally it’s time for…
The Script
Function Set-SPGlobalNav { <# .SYNOPSIS Adds a heading and links to the Global Navigation .DESCRIPTION Set-SPGlobalNav creates a heading through the provided parameter and then populates the heading with link titles and URLs retrieved from a CSV file. .PARAMETER Heading A string for the title of the heading such as: "Departments" .PARAMETER Links A string to the file location for the CSV file storing link titles and link URLs such as: "D:\Scripts\GlobalNavLinks.csv" .EXAMPLE This example adds the Global Navigation links to all subsites in a Web Application: Get-SPWebApplication http://portal.company.com | Get-SPSite -Limit All | Get-SPWeb -Limit All | Set-SPGlobalNav -Heading “New Heading” -Links “D:\Scripts\GlobalNavLinks.csv” .EXAMPLE This example adds the Global Navigation links to a specific site: Get-SPWeb -Identity http://portal.company.com/dept/it/helpdesk | Set-SPGlobalNav -Heading “New Heading” -Links “D:\Scripts\GlobalNavLinks.csv” #> [CmdletBinding()] param( [Parameter(Mandatory=$true, ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$True)] [Microsoft.SharePoint.PowerShell.SPWebPipeBind]$Site, [string]$Heading, [string]$Links ) BEGIN { Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue } PROCESS { $web = $site.Read() $CreateNavigationNodeMethod = [Microsoft.SharePoint.Publishing.Navigation.SPNavigationSiteMapNode]::CreateSPNavigationNode Write-Host “`nAdding the Heading Node: ” $Heading; $headingNode = $CreateNavigationNodeMethod.Invoke($Heading, [System.String]::Empty, [Microsoft.SharePoint.Publishing.NodeTypes]::Heading, $web.Navigation.TopNavigationBar) $headingCollection = $headingNode.Children $inputFile = Import-CSV $Links foreach($row in $inputFile) { Write-Host “Adding the Link: ” $row.LinkTitle $linkNode = $CreateNavigationNodeMethod.Invoke($row.LinkTitle, $row.LinkURL, [Microsoft.SharePoint.Publishing.NodeTypes]::AuthoredLinkPlain, $headingCollection) $linkNode.Update() } } }
_________________________________________
09/11/2012 at 12:02 pm
Great Script. Any chance you could show me how to extend it to create multiple heading/link groups thus building out the entire global navigation. The use of multiple CVS files is just fine.
09/27/2012 at 9:45 am
Hey Matt, if you can stomach moving from csv to xml files, I can help you out, or even point you to another post: http://blog.mastykarz.nl/programmatically-configuring-menu-items-sharepoint-2010/ (Hope you don’t mind me linking, Adam!)
09/27/2012 at 9:50 am
I don’t mind! It’s all about growing the community. Thanks for helping him out.
09/20/2012 at 5:49 am
This is something I’ve needed to do since discovering that site collection is as high as global navigation goes (schoolboy error, I know, but I know it now 😉 ).
Awesome work – thanks for sharing, too!
09/20/2012 at 6:32 am
You are welcome and thanks for visiting the site! I know, that is a pain. Hopefully that will be resolved with the new Managed Metadata based navigation in SP2013.
11/25/2013 at 3:03 pm
Hi Adam,
How would you remove all nodes first, before using this script to add them?
Using your script as a base, I’m building a script that would remove and replace the navigation across a whole web application should the navigation need to change.
11/26/2013 at 5:00 am
Reblogged this on ashley.geek.nz and commented:
After searching for quite some time, finally found this script snippet to add to the Enterprise Publishing site top bar navigation… now just to put some deletion logic in front of it, and I’ll have a workaround to the lack of global nav in SP2010 with multiple Site Collections.
11/27/2013 at 5:38 am
How can i open the TCSC in new Tab when we click on the TCSC reply as soon as possible
11/27/2013 at 6:32 am
I’m not sure, actually. Perhaps there is a property on the node that can be set to open in new window.
06/04/2014 at 9:11 am
can you please tell me which property is used to open in new window it’s very urgent for me
09/12/2014 at 10:57 am
Great solution works on sharepoint 2013 too!
09/12/2014 at 10:58 am
Great Solution! Works on SharePoint 2013 too!