none
How to Programmatically Add a Global Navigation Link in Sharepoint 2013 Online 365 using Powershell 5

    Question

  • I am trying to modify a PowerShell script to add Links to the Sharepoint 365 online Global Navigation bar of my sites. I currently have a script which will add a Header to the Global Navigation Bar, but rather than add headers, I need to modify it to add “Links”.

    Referencing the diagram below, I need the icon for the new link to look like the example “Link1” Currently I can only add a header with the attached script which appears like the example “Hello1” below. Does anyone know how I can modify my script to make the Global Navigation item appear as a link?

    Script which adds a “Header”

    siteUrl = $_.Url

    $siteTitle = $_.Title

    $context = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)

    $context.Credentials = $credentials

    $context.RequestTimeOut = 5000 * 60 * 10;

    $web = $context.Web

    $site = $context.Site

    $context.Load($web)

    $context.Load($site)

    $context.ExecuteQuery() 

    $NavBar = $context.Web.Navigation.TopNavigationBar

    $NavigationNode = New-Object Microsoft.SharePoint.Client.NavigationNodeCreationInformation

    $NavigationNode.Title = $ItemName

    $NavigationNode.Url = $ItemUrlPath

    $NavigationNode.AsLastNode = $true          

    $context.Load($NavBar.Add($NavigationNode))

    $context.ExecuteQuery()  


    gm.ca

    Tuesday, February 9, 2016 10:03 PM

All replies

  • Hi,

    check this script from this blog

    $w = get-spweb http://takana
    $l = New-Object Microsoft.SharePoint.Navigation.SPNavigationNode("Smells like team spirit", "/pages/teamspirit.aspx")
    $w.Navigation.TopNavigationBar.AddAsLast($l)

    http://chuvash.eu/2012/01/02/add-global-navigation-links-in-powershell/


    Kind Regards,

    John Naguib

    Senior Consultant

    John Naguib Blog John Naguib Twitter

    Please remember to mark this as answered if it helped you

    Wednesday, February 10, 2016 12:00 PM
  • Thanks but in this SharePoint 2013 online environment, we have no way to load PSSnapins, hence Get-SpWeb is not a valid command.

    gm.ca

    Wednesday, February 10, 2016 1:31 PM
  • you can use Get-SPOSite or follow this for using get-spweb 

    http://sharing-the-experience.blogspot.com/2013/10/sharepoint-online-and-powershell-how-to.html


    Kind Regards,

    John Naguib

    Senior Consultant

    John Naguib Blog John Naguib Twitter

    Please remember to mark this as answered if it helped you

    Wednesday, February 10, 2016 1:38 PM
  • The entire script is built for the Server Side Object Model, sadly it's a total dead end for SharePoint Online. GM.CA's script is far better as a starting point.

    I believe what's happening is that as you're specifying that this is created as the last node it is automatically creating the link as a header. If you can find a way to select a node already in the navigation to append your link to then you should be able to create it as a sub node.

    Wednesday, February 10, 2016 3:29 PM
  • Thanks John and Alex. I am already able to create sub-nodes which appear as "Links" beneath the Headings using additional code. However, we need them to appear on the first level of the Global Navigation Bar as "Links" if we have no plans to add sub links beneath them.

    If I could find a way to make the script add the node as a child to the Global Navigation Bar, I believe that it would create it as a Link, but since the Global Navigation Bar is a "Container" rather than a "Heading", I don't know of a way to add the new node as a child to the container.


    gm.ca

    Wednesday, February 10, 2016 4:02 PM
  • This function will add child nodes below existing Headers and add them as type: "Link":

    Function AddNavigationNode([Microsoft.SharePoint.Client.NavigationNodeCollection]$Nodes,[string]$Title,[string]$Url){
       $context = $Nodes.Context
       $Node = New-Object Microsoft.SharePoint.Client.NavigationNodeCreationInformation
       $Node.Title = $Title
       $Node.Url = $Url
       $Node.AsLastNode = $true
       $context.Load($Nodes.Add($Node))
       $context.ExecuteQuery()
    }

    To run this I must first run the following function which will determine the Parent nodes below which to add the child nodes based on the parameter "Title" which is the targeted parent that I want to add the child node to.

    Function FindNavigationNodeByTitle([Microsoft.SharePoint.Client.NavigationNodeCollection]$Nodes,[string]$Title)
    {
          $context = $Nodes.Context
          $context.Load($Nodes)
          $context.ExecuteQuery()
          $node = $Nodes | Where-Object { $_.Title -eq $Title }
          return $node
    }

    Is there some way to modify these functions to add the node as a "Link" at the top level of the Global Navigation Bar?

    Thanks to http://sharepoint.stackexchange.com/users/10610/vadim-gremyachev

    for providing the function to create the child nodes.








    gm.ca


    Wednesday, February 10, 2016 4:24 PM