Tuesday, August 7, 2012

How To Add Heading and Links to 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, what you’ve all been waiting 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();
 
       }
 
    }
}



Source: http://www.tcscblog.com

No comments:

Post a Comment