Event Receiver to Remove “Recent” from SP2013 Quick Launch

I’m sure removing the Recent heading from the Quick Launch in SharePoint 2013 has been talked about a million times over since SharePoint 2013 was launched. It’s been solved in this way and that way, by hand, with javascript and programmatically. In this post I share the code to remove the heading with the ListAdded event receiver.

With and without the Recent heading on the Quick Launch navigation.
With and without the Recent heading on the Quick Launch navigation.

Event receiver code

It’s based on code provided as an answered on the SharePoint StackExchange website by Remko van Laarhoven. I’ve then wrapped in a list added event receiver so that it is executed each time a new list or library is added. As the list/library is then not available on the Quick Launch I’ve added a couple of lines to then show it.

Update (22nd June 2014): since creating this solution I have since discovered that the Recent heading still is created even with the event receiver triggering when lists/libraries created from templates. I resolved this by adding a sleep before the code to remove the heading is executed.
[code language=”c”]System.Threading.Thread.Sleep(1500);[/code]

This in conjunction with the jQuery method prevent users from ever seeing the Recent Heading whether they are viewing the page or editing the links on the page.
[code language=”js”]
// Hide Recent on Quick Launch
$("#ctl00_PlaceHolderLeftNavBar_QuickLaunchNavigationManager .ms-core-listMenu-root li:contains(‘Recent’)").children().remove();
$("#ctl00_PlaceHolderLeftNavBar_QuickLaunchNavigationManager .ms-core-listMenu-item:contains(‘Recent’)").remove();
[/code]

[code language=”c”]
public class ListAddedEventReceiver : SPListEventReceiver
{
public override void ListAdded(SPListEventProperties properties)
{
base.ListAdded(properties);
SPWeb web = properties.Web;
if (web != null)
{
//Sleep
System.Threading.Thread.Sleep(1500);

//Remove heading
var title = SPUtility.GetLocalizedString("$Resources:core,category_Recent", null, web.Language);
SPNavigationNodeCollection nodes = web.Navigation.QuickLaunch;
foreach (SPNavigationNode node in nodes)
{
if (node.Title.ToLower().Equals(title.ToLower()))
{
// Delete the recent heading node
node.Delete();
break;
}
}
// Show list on the quick launch
SPList list = web.Lists[properties.ListId];
list.OnQuickLaunch = true;
list.Update();
}
}
}
[/code]

Download Remove Recent Heading Solution

For those who don’t want to create the event receiver themselves in Visual Studio or don’t know how to, I have a packaged the solution so that you can deploy the WSP to your environment. For those who don’t know how to use this code, I will write a post explaining how to create this event receiver using Visual Studio from an IT Pros perspective very soon.

jcallaghan.removerecentheading.wsp

As with anything you download from the internet remember to review, rename and test this code/solution before using it in a production environment.

Security trimmed top navigation links

I was asked to review a client environment yesterday to find out why the links in their top navigation bar were displaying for users that did not have permission to the particular sites.

Creating sites

It turns out that when sites were being created by the client on SharePoint Foundation 2010 they were being created without the ‘include on the top navigation bar’ check box ticked. As a result the link was then not automatically added to the top navigation bar but instead later manually added and so was not a security trimmed link.

Display this site on the top link bar of the parent site
Display this site on the top link bar of the parent site

It was then after removing permissions to the various sites that it became clear that users were able to see the top navigation bar link to the sites even though they did not have access.

Obviously there are situations when users don’t have permissions to a site and you don’t want them to see that the site even exists. An example of this might be in an extranet scenario, when you have third parties accessing project sites and you don’t want those third parties seeing the names of other project sites that may exist let alone the content…so how do we prevent this?

Identifying security trimmed links

By reviewing the URLs of the links in top navigation bar I was able to identify whether the links were security trimmed or not. If the field for the URL is disabled then the link is security trimmed and most probably created when as site was created.

Custom top navigation link that is not security trimmed
Custom top navigation link that is not security trimmed
Security trimmed top navigation link
Security trimmed top navigation link

Adding new security trimmed links

After identifying the problem, I then had to make the existing links security trimmed. I did this in two stages. The first was to make a note of the position of the link that needed to be replaced. I then deleted it from the top navigation bar using the ‘Top Link Bar’ site settings page (_layouts/topnav.aspx). The second stage was then to create the new security trimmed link by using the PowerShell code below.

[code lang=”powershell”]
$SPWeb = Get-SPWeb http://demo.jcallaghan.com/
$Nav = $SPWeb.Navigation.TopNavigationBar
$NewLink = New-Object Microsoft.SharePoint.Navigation.SPNavigationNode -argumentlist @("Site Name", "/sitename/default.aspx")
$Nav.AddAsLast($NewLink)
[/code]

Modify the $SPWeb and @(“Site Name”, “/sitename/default.aspx”) arguments as required and run the code for each of the top navigation bar links that need to be security trimmed. Remember the old link will need to be removed and the new one ordered as required.

Conclusion

It appears SharePoint, specifically SharePoint Foundation 2010 only honours security trimmed links in the top navigation when the links are created automatically as opposed to being created manually.

Note: this post specifically targets SharePoint 2010 Foundation which does not include the extended navigation that is included as part of the Publishing feature.

Clear the SharePoint Quick Launch using PowerShell

Today I had a requirement to remove all the headings and links from the quick launch navigation of hundreds of SharePoint sites. The sites were being provisioned as part of a PowerShell deployment script that was deleting the default list and libraries. Going through each of these sites manually was not an option – so I edited the deployment script to include a function to remove the headings for me.

SharePoint Quick Launch with Headings
SharePoint Quick Launch with Headings

I remembered doing something similar to this back on SharePoint 2007 but I didn’t have access to the previous script or project and instead had to research the subject for a while to find what I needed.

Solution

A post from Get-SPScripts supplied me with what I was after, although it was part of a much larger script. So I picked away at their code and made it into the following PowerShell function to re-use in other projects.

[code lang=”powershell”]
function Remove-SPQuickLaunchLinks($url){
$SPWeb = Get-SPWeb $url
$QuickLaunch = $SPWeb.Navigation.QuickLaunch
$Nodes = @()

$QuickLaunch | ForEach-Object {
$Nodes = $Nodes + $_.Id
}

$Nodes | ForEach-Object {
$Node = $SPWeb.Navigation.GetNodeById($_)
$Node.Delete()
}
$SPWeb.Dispose()
}

Remove-SPQuickLaunchLinks "http://sharepoint.jcallaghan.com/"
[/code]

The above Remove-SPQuickLaunchLinks function will remove all headings and links from the SharePoint quick launch for a particular site.

Empty SharePoint Quick Launch
Empty SharePoint Quick Launch

Download

You can download the script here. Remember to review, rename and test this script before using it in a production environment.