Thursday, 15 September 2011

Hide Left Navigation Menu in new pages for Sharepoint 2010 with Visual Studio 2010

When you create an application page with Visual Studio 2010 you will notice that when you deploy it the left menu sticks there, and there is no way to hide it.

In order to play around with the controls, we will have to have a look to the master page and find the controls involve the whole menu.

That is what I did, and I found (this can be done with any other menu) this controls:

<asp:ContentPlaceHolder id="PlaceHolderLeftNavBar" runat="server">
	<div class="ms-quicklaunchouter">
	<div class="ms-quickLaunch">
	<asp:ContentPlaceHolder id="PlaceHolderQuickLaunchTop" runat="server">
	<SharePoint:UIVersionedContent UIVersion="3" runat="server">
	<h3 class="ms-standardheader"><label class="ms-hidden"><SharePoint:EncodedLiteral runat="server" text="<%$Resources:wss,quiklnch_pagetitle%>" EncodeMethod="HtmlEncode"/></label>
	<Sharepoint:SPSecurityTrimmedControl runat="server" PermissionsString="ViewFormPages">
		<div class="ms-quicklaunchheader"><SharePoint:SPLinkButton id="idNavLinkViewAll" runat="server" NavigateUrl="~site/_layouts/viewlsts.aspx" Text="<%$Resources:wss,quiklnch_allcontent%>" accesskey="<%$Resources:wss,quiklnch_allcontent_AK%>"/></div>

Have a look to the first line. That is where we have to work, we basically have to hide all the stuff is inside, How? well, think about it, if we overwrite that control after the master page is called and leave it empty, we have the job done. Easy isn’t it? It even easier to build it.

Ok, let’s go to do one of my quick “Step By Steps” solutions, at the end of the article you will be able to download the project.

1- Go to Visual studio 2010->New project->Empty Sharepoint Project-> call it netsourcecodeLeftMenu.

2- Right click in the project and Add –> New Item… Select application page, leave the default name ApplicationPage1

3- Go to Features and rename it with this name: HideLeftMenuFeature

4- Your current project should look like this now:


5- Go to ApplicationPage1.aspx, edit the page and add these lines below the Main Content:

<asp:Content ID="Content1"  ContentPlaceHolderId="PlaceHolderLeftNavBar" runat="server"></asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderId="PlaceHolderNavSpacer" runat="server"></asp:Content> 

6- Your page should look like this then:

<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Import Namespace="Microsoft.SharePoint.ApplicationPages" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ApplicationPage1.aspx.cs" Inherits="netsourcecodeLeftMEnu.Layouts.netsourcecodeLeftMEnu.ApplicationPage1" DynamicMasterPageFile="~masterurl/default.master" %>
<asp:Content ID="PageHead" ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">
<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
<asp:Content ID="Content1"  ContentPlaceHolderId="PlaceHolderLeftNavBar" runat="server"></asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderId="PlaceHolderNavSpacer" runat="server"></asp:Content> 
<asp:Content ID="PageTitle" ContentPlaceHolderID="PlaceHolderPageTitle" runat="server">
Application Page
<asp:Content ID="PageTitleInTitleArea" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server" >
My Application Page

7- Deploy the solution and go to the page : http://<yourserver>/<yoursitecollection>/_Layouts/netsourcecodeLeftMEnu/ApplicationPage1.aspx

8- This is what you should get:


Conclusion: As you can see the possibilities are endless for web developers. By working like this the logic can be integrated and hosted in Sharepoint, but the UI doesn’t need to be the same. The users don’t need to see that your page is part of the system.


No comments: