1

I am creating some Global / Current navigations nodes on a SharePoint site (SharePoint 2010 in house site) by fetching them through a csv file.

Below is the script:

#Script to create Global/Current navigation links 

$Web = Get-SPWeb "http://...."
$pubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)

Function Set-SPGlobalNav($Links)
{

    $CreateNavigationNodeMethod = [Microsoft.SharePoint.Publishing.Navigation.SPNavigationSiteMapNode]::CreateSPNavigationNode

        $inputFile = Import-CSV $Links

        foreach($row in $inputFile) {

            Write-Host “Adding the Link: ” $row.LinkTitle

        $qlNav = $pubWeb.Navigation.GlobalNavigationNodes

        $headingNode = $CreateNavigationNodeMethod.Invoke($row.LinkTitle, $row.LinkURL, [Microsoft.SharePoint.Publishing.NodeTypes]::AuthoredLinkPlain, $qlNav) 
        $web.Update()       


        }
}

Set-SPGlobalNav -Links "c:\links.csv"

This is absolutely working fine in SharePoint 2010 site and creating navigation links as supplied in a CSV file.

Now I want same functionality in a office 365 site also. I have already done setup for SharePoint online management shell. and need to run similar kind of script. Kindly assist how can I achieve that as I am new in scripting.

Thanks in advance.

2

Unfortunately, PowerShell for O365 is not going to accomplish that. You can, however, use the CSOM to accomplish this. This link shows how to create a navigation node. Here is the main part of it:

string siteUrl = "http://server/sites/MySiteCollection";

ClientContext clientContext = new ClientContext(siteUrl);
Web site = clientContext.Web;

// Get the Quick Launch navigation node collection.
NavigationNodeCollection collQuickLaunchNode = site.Navigation.QuickLaunch;

// Set properties for a new navigation node.
NavigationNodeCreationInformation ciNavicationNode = new NavigationNodeCreationInformation();
ciNavicationNode.Title = "New Node";
ciNavicationNode.Url = "http://localhost";
// Create node as the last node in the collection.
ciNavicationNode.AsLastNode = true;
collQuickLaunchNode.Add(ciNavicationNode);

clientContext.Load(collQuickLaunchNode);
clientContext.ExecuteQuery();

You can either execute this in console, or you can execute it in PowerShell, as shown in this link (you need the Microsoft.SharePoint.Client(.Runtime) dll's from the ISAPI folder in 15 hive). Here is what it would look like in PowerShell:

$username = "username"
$password = "password"
$url = "https://site.sharepoint.com/sites/appdev"
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force

Add-Type -Path (Join-Path ([System.Environment]::GetFolderPath("Desktop")) "15 Hive\ISAPI\Microsoft.SharePoint.Client.dll")
Add-Type -Path (Join-Path ([System.Environment]::GetFolderPath("Desktop")) "15 Hive\ISAPI\Microsoft.SharePoint.Client.Runtime.dll")

$clientCtx = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username,$securePassword)
$clientCtx.Credentials = $credentials

$web = $clientCtx.Web
$navColl = $web.Navigation.QuickLaunch
$newNavNode = New-Object Microsoft.SharePoint.Client.NavigationNodeCreationInformation
$newNavNode.Title = "Node Title"
$newNavNode.Url = "http://sharepoint.stackexchange.com"
$newNavNode.AsLastNode = $true
$navColl.Add($newNavNode)
# I received an error after executing the above.  
# I ignored it and moved on, and executed the following lines.  
# The navigation node was added successfully, as shown in my screen cap.     
$clientCtx.Load($navColl)
$clientCtx.ExecuteQuery()

I got an error at $navColl.Add($newNavNode), but after the $clientCtx.ExecuteQuery(), I checked my site, and the new navigation was there:

enter image description here

6
  • Hi Thanks, I am looking for a powershell solution. Please tell me that how can I execute this in Powershell?
    – Chitransh
    Apr 17 '14 at 12:31
  • Added the PowerShell script
    – wjervis
    Apr 17 '14 at 13:25
  • Hi While running the above script after editing my changes in the script (Like Url, username, password, dll path) I am getting below error in online shell management screen:format-default : The collection has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested. + CategoryInfo : NotSpecified: (:) [format-default], CollectionNo tInitializedException + FullyQualifiedErrorId : Microsoft.SharePoint.Client.CollectionNotInitial izedException,Microsoft.PowerShell.Commands.FormatDefaultCommand
    – Chitransh
    Apr 18 '14 at 8:43
  • Hi after some more modifying the script, script is running but, exactly on same step: $navColl.Add($newNavNode) I am getting error and link is not added in the navigation: You cannot call a method on a null-valued expression. At C:\Users\chsuser\Desktop\Final\Final\navigationoffice365.ps1:47 char:2 + $navColl.Add($newNavNode) + ~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull Please help me on this issue.
    – Chitransh
    Apr 18 '14 at 10:55
  • 2
    The error isn't because the action failed - but because powershell fails to write the object. Add | out-null behind the line to ignore the output error. spyankulov.blogspot.be/2014/08/… Jun 29 '15 at 15:07
1

Instead of using,

collQuickLaunchNode.Add(ciNavicationNode);
clientContext.Load(collQuickLaunchNode);

use

clientContext.Load(collQuickLaunchNode.Add(ciNavicationNode))

This will solve the error

1
  • Can you provide some explanation on why this would solve his issue?
    – wjervis
    Feb 9 '15 at 13:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.