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
- )
- #Getting SharePoint Context
- .\Get-SharePointContext.ps1
- #Calling add function
- Add-SPTopNavLinks -ctx $Spps -spWeb $web
- 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
- }
- #
- 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
- }
- $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()
- }
- }
If you have any issues / feedback, let me know.
Harshad PansuriyaPosted Oct 6, 2015, 8:58 AM
Nice one
Santhakumar MunuswamyPosted Oct 5, 2015, 11:59 PM
Nice Share
Abhay ShankerPosted Oct 4, 2015, 4:01 AM
Nice Share.
RakeshPosted Oct 3, 2015, 11:20 PM
Good share
Nilesh JadavPosted Oct 3, 2015, 9:30 PM
Good Post
Pankaj Kumar ChoudharyPosted Oct 3, 2015, 8:34 PM
Nice Explain Rahul.......
Sumit JoshiPosted Oct 3, 2015, 4:23 PM
thanks for sharing.