CHUVASH.eu

CHunky Universe of Vigourous Astonishing SHarepoint :)

Add global navigation links in Powershell and Feature Receiver

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:

<!--Meglerfront: Page Navigation-->
<Feature ID="7829235a-ffb3-4ddf-0b5b-2a1d79668aa5">
  <Properties xmlns="http://schemas.microsoft.com/sharepoint/">
    <Property Key="MyGlobalLinks"
     Value="Salam;/$Resources:cmscore,List_Pages_UrlName;/salam.aspx;#Ciper;/$Resources:cmscore,List_Pages_UrlName;/ciper.aspx" />
  </Properties>
</Feature>

In the example no error handling is provided for the sake of brevity.

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;.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Вула Чăвашла

VulaCV - Чăвашла вулаттаракан сайт

Discovering SharePoint

And going crazy doing it

Bram de Jager - Coder, Speaker, Author

Office 365, SharePoint and Azure

SharePoint Dragons

Nikander & Margriet on SharePoint

Paul J. Swider - RealActivity

RealActivity is a specialized healthcare services and solution advisory firm.

Mai Omar Desouki - Avid SharePointer

Egyptian & Vodafoner - Senior SharePoint Consultant

Cameron Dwyer | Office 365, SharePoint, Outlook, OnePlace Solutions

Office 365, SharePoint, OnePlace Solutions & Life's Other Little Wonders

paul.tavares

Me and My doings!

Share SharePoint Points !

By Mohit Vashishtha

Jimmy Janlén "Den Scrummande Konsulten"

Erfarenheter, synpunkter och raljerande om Scrum från Jimmy Janlén

Aryan Nava

| Blockchain | Serverless Architecture | Microservices Architecture | DevOps | AWS Lambda | Teraform |

SPJoel

SharePoint for everyone

SharePointRyan

Ryan Dennis is a SharePoint Solution Architect with a passion for SharePoint and PowerShell

SharePoint 2020

The Vision for a Future of Clarity

Aharoni in Unicode

Treacle tarts for great justice

... And All That JS

JavaScript, Web Apps and SharePoint

blksthl

Mostly what I know about SharePoint - CommunicoCuspis

%d bloggers like this: