Add Multiple Top Navigation Links With Remote PowerShell

Recently, I published two articles related to how to work with SharePoint remotely with help of PowerShell. Their is Client Side SharePoint PowerShell libraries available to use which help you to get start. In my article Getting SharePoint Context with Client Side SharePoint PowerShell, I explained how to use this Client Side SharePoint PowerShell and created re-usable PowerShell (attached to that article) which helps you to create SharePoint Client Context. This PowerShell will help you build your own PowerShell libraries which could be reused and save lot of time.

Another article Adding Bulk Attachments to SharePoint List Item With PowerShell Remotely use those libraries to add bulk attachments to particular list item.

In this article, I will help you to learn how this remote SharePoint PowerShell is used to add multiple top navigation links to particular SharePoint Site (SPWeb) with input of CSV file. Again I am using existing Get-SharePointContext.ps1 file and building on top of that.

Purpose:

Adding multiple top navigation links to SharePoint site (SPWeb).

Pre-requisite

  • Client Side SharePoint PowerShell – This needs to be done only once. If you already have downloaded those PowerShell modules, ignore this.

  • Get-SharePointContext.ps1 PowerShell File – Refer article Getting SharePoint Context with Client Side SharePoint PowerShell and download the attachment in same folder as Client Side SharePoint PowerShell modules.

  • Add-SPTopNavLinks.ps1 – Attached to this article.

  • Sample CSV file – Attached to this article.

Add-SPTopNavLinks.ps1 contains generic PowerShell function which takes input as Microsoft.SharePoint.Client.ClientContext and Microsoft.SharePoint.Client.Web objects.

  1. Param  
  2. (  
  3.    [Parameter(Mandatory=$true, Position=0)]  
  4.    [ValidateNotNullOrEmpty()]  
  5.    [Microsoft.SharePoint.Client.ClientContext] $ctx,  
  6.   
  7.    [Parameter(Mandatory=$true, Position=1)]  
  8.    [ValidateNotNullOrEmpty()]  
  9.    [Microsoft.SharePoint.Client.Web] $spWeb  
  10. )   
These objects are created with the help of Get-SharePointContext.ps1 file which tries to understand your Target SharePoint environment and permissions to access that environment and target site URL. Once you have context and Web object, it is passed to another PowerShell function: Add-SPTopNavLinks.
  1. #Getting SharePoint Context  
  2. .\Get-SharePointContext.ps1  
  3. #Calling add function  
  4. Add-SPTopNavLinks -ctx $Spps -spWeb $web   
Add-SPTopNavLinks function request user to input path of the CSV file which contains link details to be added on target SharePoint site. CSV file can be named anything but should have the following headers:

 

  • LinkTitle
  • LinkUrl
  • LinkOrder

It is not necessary that you should use above headers. You can use your own header names provided you update Add-SPTopNavLinks function accordingly before using it.

The function imports the CSV file in a PowerShell object. Before importing filters the CSV file to include only records whose LinkTitle column is not blank or empty and then finally sort the list based on LinkOrder column. Note that LinkOrder column type is changed to [int] before sorting happens on the values of LinkOrder.

  1. #  
  2. Importing csv file and Filtering the list based on LinkTitle column not empty and then sorting based on LinkOrder  
  3. $csvFile = Import - Csv - Path $userPath | Where - Object {  
  4.     $_.LinkTitle - ne $null - and $_.LinkTitle - ne ""  
  5. } | Sort - Object {  
  6.     [int] $_.LinkOrder  
  7. }  
After that it gets the SharePoint Top Navigation object.
  1. #  
  2. Getting SharePoint Navigation object  
  3. $webNav = $spWeb.Navigation  
  4. $ctx.Load($webNav)  
  5. $ctx.ExecuteQuery()  
  6. if ($webNav - ne $null) {  
  7.     $webTopNav = $webNav.TopNavigationBar  
  8.     $ctx.Load($webTopNav)  
  9.     $ctx.ExecuteQuery()  
  10.     Write - Host  
  11.     Write - Host "Number of top navigation links found:"  
  12.     $webTopNav.Count  
  13. }  
Function prompts user to decide whether he/she want to delete existing links before adding new links. If user decide to delete all existing links, then function deletes those links. If not, it adds new links at the end of existing links.
  1. $delExistingNav = Read - Host "Do you wanto delete existing top navigation links (Y/N)"  
  2. Write - Host  
  3. if ($delExistingNav - eq "Y" - or $delExistingNav - eq "y") {  
  4.     Write - Host - ForegroundColor Yellow "Deleting existing top navigation links..."  
  5.     Write - Host  
  6.     for ($i = $webTopNav.Count - 1; $i - ge 0; $i--) {  
  7.         Write - Host "Deleting "  
  8.         $webTopNav[$i].Title  
  9.         $webTopNav[$i].DeleteObject()  
  10.     }  
  11.   
  12. }  
  13. foreach($entry in $csvFile) {  
  14.   
  15.     Write - Host  
  16.     Write - Host - ForegroundColor Cyan "Processing link:"  
  17.     $entry.LinkTitle  
  18.     Write - Host - ForegroundColor Cyan "`t LinkUrl:"  
  19.     $entry.LinkUrl  
  20.     Write - Host - ForegroundColor Cyan "`t LinkOrder:"  
  21.     $entry.LinkOrder  
  22.     try {  
  23.         $newNavNode = New - Object Microsoft.SharePoint.Client.NavigationNodeCreationInformation  
  24.         $newNavNode.Title = $entry.LinkTitle  
  25.         $newNavNode.Url = $entry.LinkUrl  
  26.         $newNavNode.AsLastNode = $true  
  27.   
  28.         $SPPS.Load($webTopNav.Add($newNavNode))  
  29.         try {  
  30.             $SPPS.ExecuteQuery()  
  31.         } catch {#  
  32.             In  
  33.             case of SP2010,  
  34.             if link is external to the site, it throws file not found exception so below handling  
  35.             $newNavNode.IsExternal = $true  
  36.             $ctx.Load($webTopNav.Add($newNavNode))  
  37.             $ctx.ExecuteQuery()  
  38.         }  
  39.         Write - Host - ForegroundColor Green "`t Top navigation link "  
  40.         $entry.LinkTitle " added successfully."  
  41.     } catch {  
  42.         Write - Host Error adding top navigation link - ForegroundColor Cyan $entry.LinkTitle Failed with error: $_.Exception.ToString()  
  43.     }  
  44.   
  45. }  
Note: In case of SP2010, if link Url is of external content, it throws FileNotFound exception. So it is handled in catch block to specify that link is external.

If you have any issues / feedback, let me know.