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.
- Param
- (
- [Parameter(Mandatory=$true, Position=0)]
- [ValidateNotNullOrEmpty()]
- [Microsoft.SharePoint.Client.ClientContext] $ctx,
-
- [Parameter(Mandatory=$true, Position=1)]
- [ValidateNotNullOrEmpty()]
- [Microsoft.SharePoint.Client.Web] $spWeb
- )
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. - #Getting SharePoint Context
- .\Get-SharePointContext.ps1
- #Calling add function
- 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.
- #
- Importing csv file and Filtering the list based on LinkTitle column not empty and then sorting based on LinkOrder
- $csvFile = Import - Csv - Path $userPath | Where - Object {
- $_.LinkTitle - ne $null - and $_.LinkTitle - ne ""
- } | Sort - Object {
- [int] $_.LinkOrder
- }
After that it gets the SharePoint Top Navigation object.
- #
- Getting SharePoint Navigation object
- $webNav = $spWeb.Navigation
- $ctx.Load($webNav)
- $ctx.ExecuteQuery()
- if ($webNav - ne $null) {
- $webTopNav = $webNav.TopNavigationBar
- $ctx.Load($webTopNav)
- $ctx.ExecuteQuery()
- Write - Host
- Write - Host "Number of top navigation links found:"
- $webTopNav.Count
- }
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.
- $delExistingNav = Read - Host "Do you wanto delete existing top navigation links (Y/N)"
- Write - Host
- if ($delExistingNav - eq "Y" - or $delExistingNav - eq "y") {
- Write - Host - ForegroundColor Yellow "Deleting existing top navigation links..."
- Write - Host
- for ($i = $webTopNav.Count - 1; $i - ge 0; $i--) {
- Write - Host "Deleting "
- $webTopNav[$i].Title
- $webTopNav[$i].DeleteObject()
- }
-
- }
- foreach($entry in $csvFile) {
-
- Write - Host
- Write - Host - ForegroundColor Cyan "Processing link:"
- $entry.LinkTitle
- Write - Host - ForegroundColor Cyan "`t LinkUrl:"
- $entry.LinkUrl
- Write - Host - ForegroundColor Cyan "`t LinkOrder:"
- $entry.LinkOrder
- try {
- $newNavNode = New - Object Microsoft.SharePoint.Client.NavigationNodeCreationInformation
- $newNavNode.Title = $entry.LinkTitle
- $newNavNode.Url = $entry.LinkUrl
- $newNavNode.AsLastNode = $true
-
- $SPPS.Load($webTopNav.Add($newNavNode))
- try {
- $SPPS.ExecuteQuery()
- } catch {#
- In
- case of SP2010,
- if link is external to the site, it throws file not found exception so below handling
- $newNavNode.IsExternal = $true
- $ctx.Load($webTopNav.Add($newNavNode))
- $ctx.ExecuteQuery()
- }
- Write - Host - ForegroundColor Green "`t Top navigation link "
- $entry.LinkTitle " added successfully."
- } catch {
- Write - Host Error adding top navigation link - ForegroundColor Cyan $entry.LinkTitle Failed with error: $_.Exception.ToString()
- }
-
- }
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.