Programmatically Show\Hide or Change links in quick launch bar

You can programmatically Show\Hide or Change Url for the links in Quick Launch bar according to the logged in User or Whatever your condition is. In this post however, we will just Change the Url for one of links in quick launch bar, according to the logged in user's group. The Code is written in a User Control which will later be added to the master page. Adding the Control in the master page does add some overhead but will allow the code to run on each sharepoint page.

Initial requirement -

Change the Url for one of the links called "Your Department" in quick launch bar according to the logged in user's group.

Steps :

1. Create Webapplication project and add a Web UserControl in it.

2. Add References to Microsoft.SharePoint dll.

Use the below method in Page load to Change the url:

private void ChangeLink(string NewLink)
{

SPSite site = SPContext.Current.Site;
SPWeb myWeb = site.RootWeb;

SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite siteCollection = new SPSite(site.ID))
{
using (SPWeb web = siteCollection.OpenWeb(myWeb.ID))
{
web.AllowUnsafeUpdates = true;

SPNavigationNodeCollection nodes = web.Navigation.QuickLaunch;

foreach (SPNavigationNode node in nodes)
{
CheckURL(node,web, NewURl);
}

web.AllowUnsafeUpdates = false;
} }
});

}

private void CheckURL(SPNavigationNode Node,SPWeb web,string NewURl)
{

if (Node.Children.Count > 0)
{
SPNavigationNodeCollection childNodes = Node.Children;
foreach (SPNavigationNode cNode in childNodes)
{
if (cNode.Title.ToString() == "Your Department")
{
cNode.Url = NewURl;
cNode.Update();
}
}
}
}

In above Code CheckURL method will navigate through each link in the quick launch bar to find the one with name "Your Department". If the link is found we can simply use cNode.Url and cNode.Update() to update the url.

3. After, you are done writing the logic, just Sign the Project and Build it. Dont forget to drop the signed dll in GAC.

4.. Finally, add the user Control in the Master Page and Test it.

3 comments:

  1. Hi

    I read this post 2 times. It is very useful.

    Pls try to keep posting.

    Let me show other source that may be good for community.

    Source: Bar interview questions

    Best regards
    Jonathan.

    ReplyDelete
  2. hey check this new website www.countcode.com. It's a social network made for programmers, where you can download,share or upload source codes, where you can count your own code lines for free. You have access to the web forum and the web chatroom. we are happy to have you joined to our community!

    ReplyDelete
  3. hey check this new website www.countcode.com. It's a social network made for programmers, where you can download,share or upload source codes, where you can count your own code lines for free. You have access to the web forum and the web chatroom. we are happy to have you joined to our community!

    ReplyDelete

Disclaimer

This is a personal weblog. The opinions expressed here represent my own and not those of my employer or anyone else. Should you have any questions or concerns please e-mail me at sharepointprogrammingblogger@gmail.com .

Copyright (c) 2010 @ myshaepointwork.blogspot.com. All rights are reserved.Do Not Copy.

@ Learning SharePoint.com