December 6, 2016

// // Leave a Comment

Create multiple top navigation with CSV data source using Powershell script

1) In this post we will see how to create the top navigation with CSV File as data source using powershell script

2) Also refer below powershell scripts

Bulk Creation of Site Columns:
 http://www.sharepointbasic.com/2016/11/powershell-script-to-create-bulk-site_27.html

Bulk Creation of Site Content Types:  
 http://www.sharepointbasic.com/2016/12/powershell-script-to-create-bulk-site.html

Bulk Lists and Libraries in SharePoint
http://www.sharepointbasic.com/2016/12/powershell-script-to-create-bulk-lists.html

Bulk File upload to sharepoint library
http://www.sharepointbasic.com/2016/12/upload-multiplebulk-files-to-sharepoint.html

Create multiple columns inside SharePoint List using Powershell
http://www.sharepointbasic.com/2016/12/create-multiple-columns-inside.html

Upload List Template and Create List based on List Template using PowerShell
http://www.sharepointbasic.com/2016/12/upload-list-template-and-create-list.html

3) Create a CSV File like below


4) Create a script using below code
#Author: Prem Kumar
#Date: Feb 11 2015

param(
 
    [string]$subsiteurl = $(Read-Host -prompt "Enter Web Application Root URL...?"),
    [string]$CsvFilePath = $(Read-Host -prompt "Enter the CSV"),
    [string]$Heading,
    [string]$Links
)


Function Set-SPGlobalNav {

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


$web = Get-SPWeb $subsiteurl
$SPPubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)
$SPPubWeb.Navigation.InheritGlobal = $false
$SPPubWeb.Navigation.GlobalIncludeSubSites = $true
$SPPubWeb.Navigation.GlobalIncludePages = $false
#Deletes old navigation data
$nodeColl = $web.Navigation.TopNavigationBar
[int]$count=$nodeColl.Count     
while($count -ne 0)
{
    $nodeColl[$count-1].Delete()
    $count--
}
$nav_1= $CsvFilePath+"\Navigation.csv"
Set-SPGlobalNav -Site $web -Heading "TopHeader" -Links $nav_1
$Web.Dispose()


0 comments:

Post a Comment