Monday, 11 March 2013

Add SharePoint 2010 quick launch toggle on/off feature

I quickly went over how you can add a toggle button allowing your users to easily show and hide SharePoint v4.master quick launch to maximize the use of the main content area.

That approach assume you had to have Project Server 2010 installed. In this sample I will show you how you can achieve just that with just a regular SharePoint Foundation or SP 2010 Standard, even in the cloud!
I’m going to use SharePoint Designer for this example, and if there is enough interest from you, I will post a solution on how this customization can be ported to Visual Studio 2010 solution and deployed in a cloud or farm.
1. Open SharePoint Designer 2010 and select your team site, in my case http://intranet.contoso.com
2. Click Masterpages in the left panel of SPD under Site Objects.
3. Select v4.master and check it out. Open the file for editing.
4. Locate the following line of code:
<div id="s4-leftpanel-content">
and add below right after it:
<a onclick="ExpandCollapseQuickLaunch(); return false;" href="#">
<img id="LeftPaneCollapseImg" style="padding-left: 8px;
padding-right: 8px;" src="/_layouts/images/mewa_left.gif" border="0"/>
</a>
This will take care of the collapsing the quick launch, if you don’t have mewa_left.gif you can use any other left arrow image.
5. Locate the following line of code:
<a name="mainContent"></a>
and add below right after it:
<a onclick="ExpandCollapseQuickLaunch(); return false;" href="#">
<img id="LeftPaneExpandImg" style="display: none;"
src="/_layouts/images/mewa_right.gif" border="0"/>
</a>
This will take care of getting the quick launch back once it’s hidden.
6. Now let’s add the reference to the JavaScript that will go right before the </head>:
<script type="text/javascript">
var quickLaunchVisible=true;
var showECIcon;
var contentTableMargin;
function ExpandCollapseQuickLaunch()
{
ToggleQuickLaunch(quickLaunchVisible, true);
}
function ToggleQuickLaunch(toggleOff, showECIcon)
{
if (toggleOff==null)
{
  toggleOff=true;
}
if (showECIcon==null)
{
  showECIcon=false;
}
var leftPanel = $get("s4-leftpanel");
if (leftPanel !=null)
{
  leftPanel.style.display=toggleOff ? 'none' : '';
  var contentTable = $get("MSO_ContentTable");
if (contentTable !=null)
{
 if (toggleOff)
 {
 contentTableMargin=(window.document.dir !='rtl') ? 
contentTable.currentStyle.marginLeft :
 contentTable.currentStyle.marginRight;
 }
 if (window.document.dir !='rtl')
 {
 contentTable.style.marginLeft=toggleOff ?
'0px' : contentTableMargin;
 }
 else
 {
 contentTable.style.marginRight=toggleOff ?
'0px' : contentTableMargin;
 }
}

var collapsePanelIcon=$get("LeftPaneCollapseImg");
var expandPanelIcon=$get("LeftPaneExpandImg");
expandPanelIcon.style.display=showECIcon &&
toggleOff ? '' : 'none';
if (collapsePanelIcon !=null)
{
 collapsePanelIcon.style.display=showECIcon &&
!toggleOff ? '' : 'none';
}
quickLaunchVisible=!toggleOff;
}
}
</script>
7. Save the masterpage in SharePoint Designer and refresh the page on your team site, in my case http://intranet.contoso.com you will see a small arrow just like yesterday.
Enjoy!

1 comment:

  1. is there a way to page load with the QL collapsed?

    ReplyDelete