Add Heading and Links to the Global Navigation using PowerShell

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()
 
        }
    
     }
} 



_________________________________________

Advertisements
,

Subscribe

Subscribe to our RSS feed and social profiles to receive updates.

12 Comments on “Add Heading and Links to the Global Navigation using PowerShell”

  1. Matt Says:

    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.

    Reply

  2. Paul Says:

    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!

    Reply

    • Adam Preston Says:

      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.

      Reply

  3. ubersteel Says:

    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.

    Reply

  4. ubersteel Says:

    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.

    Reply

  5. Deepak Says:

    How can i open the TCSC in new Tab when we click on the TCSC reply as soon as possible

    Reply

  6. Sal Dominguez Says:

    Great solution works on sharepoint 2013 too!

    Reply

  7. salviefresh Says:

    Great Solution! Works on SharePoint 2013 too!

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: