#Dev Import
Import-Module 'C:\Users\basantp\Documents\PNP Blog Section\Code\SharePointPnPPowerShell2013\SharePointPnP.PowerShell.2013.Commands.dll';

function CreateNavigation ($csvPath ,$siteUrl){ 
  #get and save your O365 credentials
 
	#connect to the root site collection first
	Connect-PnPOnline –Url $siteUrl -CurrentCredentials 
	
	$message  = "Connected to site : $siteUrl"
 	 $message  
	Write-Host $message  -ForegroundColor Green

	 
	$MyFile = Import-Csv $csvPath 
    # Looping the csv file collection
	foreach ($myRow in $MyFile)
	{
		$NewURL = $($myRow.SiteURL)
		$External=[System.Convert]::ToBoolean($myRow.External)
		$First=[System.Convert]::ToBoolean($myRow.First)
		try
		{ 
			$full = ($siteUrl)+ "/"+($myRow.Url);
			
			if($myRow.Header -eq ""){
				Add-PnPNavigationNode -Title  $($myRow.Title) -Url $full  -Location $($myRow.Location) -External:$External -First:$First
			
			}else{
				Add-PnPNavigationNode -Title  $($myRow.Title) -Url $full  -Location $($myRow.Location) -Header $($myRow.Header) -External:$External -First:$First
			}
			
			$message  = "Navigation Created : $($myRow.Title) and URL :$($siteUrl)/$($NewURL)for location $($myRow.Location)"
			 $message  
			Write-Host $message  -ForegroundColor Green  
			 
		}
		catch
		{
			$message  = "Unable to Create Navigation for Title: $($myRow.Title) and URL :$($siteUrl)/$($NewURL) for location $($myRow.Location)"
			 $message  
			Write-Host $message  -ForegroundColor Red  
			 "$($_.Exception.Message)" 
		}
	}  
} 

$siteUrl="http://myintranet.com/sites"
$path='C:\Users\basantp\Documents\PNP Blog Section\Code\Branding'
$inputFile =$path + "\CreateNavigation.csv"

CreateNavigation $inputFile $siteUrl