CHUVASH.eu
  • About
  • Search
X GitHub
Add global navigation links in Powershell and Feature Receiver
Posts

Add global navigation links in Powershell and Feature Receiver

Anatoly Mironov

January 2, 2012

I think, powershell is the best way to do configurations you have to do once. Adding some links to global (top) navigation is one of them:

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

The alternative is to create a web scoped feature and provide properties:

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    var web = properties.Feature.Parent as SPWeb;
    var prop = properties.Feature.Properties\["MyGlobalLinks"\];
    var links = prop.Value.Split(new\[\] { ";#" },
               StringSplitOptions.RemoveEmptyEntries);
    foreach (var item in links)
    {
        var newLink = item.Split(new\[\] { ";" },
                            StringSplitOptions.RemoveEmptyEntries);
        var newMenuItem =
                new SPNavigationNode(newLink\[0\], newLink\[1\]);
        web.Navigation.TopNavigationBar.AddAsLast(newMenuItem);
    }
}
```This feature can be prefereably hidden. The properties are passed in onet:
NavBarLink

I found even a third way to add links to global navigation: NavBarLink

<NavBars>
<NavBar Name="$Resources:core,category\_Top;"
    Separator="&amp;nbsp;&amp;nbsp;&amp;nbsp;"
    Body="&lt;a ID='onettopnavbar#LABEL\_ID#' href='#URL#' accesskey='J'&gt;#LABEL#&lt;/a&gt;"
    ID="1002">
  <NavBarLink Name="Hello" Url="/sider/Hello.aspx"/>
  <NavBarLink Name="Hello2" Url="javascript:SayHello();"/>
</NavBar>
</NavBars>
```It is much simpler, as simple as unbelievable :). Are there some problems with NavBarLinks? Edit: I found one shortcoming with NavBarLink: you can't use resources from cmscore like **$Resources:cmscore,List\_Pages\_UrlName;**. To solve we can create a copy of this string in our custom resource file: **$Resources:Takana,List\_Pages\_UrlName;**.
  • NavBarLink
  • Navigation
  • PowerShell
  • Sharepoint
  • SPFeature
  • SPWeb

Related

  • Can't add a global site navigation link
  • Retention policies
  • Uninstall custom features in a batch
  • Sharepoint Warmup Script
  • Batch remove
  • Pass arguments from/to Modal Dialog
  • Update list items with listdata.svc
  • ASP.NET Ajax Toolkit
  • Powershell scripts for AD
  • Working with web.Properties
  • Hide my site link with css
  • Configure User Profile Service Application
  • Sharepoint + OpenSource = Sant
  • Retrieve information from AD
  • Add stsadm folder to PATH in cmd
© CHUVASH.eu 2026
X GitHub