Tuesday, May 1, 2012

Display the Quick Launch in a SharePoint 2007/2010 Web Part /Publishing page

Many thanks to Christophe for a wonderful script to display quick launch in a web part page (original post/source post). I have re-written/adjusted Christophe’s code to work around issues with SharePoint 2010 upgrade. The current code works only in SharePoint 2010. I made some changes to the code to make it workable in SharePoint 2007 and 2010 pages. So, the same code works seamlessly after SharePoint 2010 upgrade. I have also simplified the code to easily understandable and to use across the site collection with some minor tweaks.
Here is the updated code:

Copy the below mentioned code into a notepad and save the file as DisplayQuickLaunch.js file. Upload the JS file your site collection level scripts/documents library.
function DisplayQuickLaunch()
{
 //Check for SharePoint Version
 var QuickLaunch =""; 
 if (typeof _fV4UI == "undefined") 
 {
  QuickLaunch = "LeftNavigationAreaCell";
 }
 else
 {
  QuickLaunch = "ctl00_PlaceHolderLeftNavBar_QuickLaunchNavigationManager";
 }
 
 var placeholder = document.getElementById(QuickLaunch);
 var quicklaunchmenu = null;
 var SourcePage = document.getElementById("SourcePage"); 
 try {
    if(SourcePage.contentDocument)
       // Firefox, Opera
 
       {quicklaunchmenu = SourcePage.contentDocument.getElementById(QuickLaunch) ;}
    else if(SourcePage.contentWindow)
       // Internet Explorer
 
       {quicklaunchmenu = SourcePage.contentWindow.document.getElementById(QuickLaunch) ;}
    else if(SourcePage.document)
       // Others?
 
       {quicklaunchmenu = SourcePage.document.getElementById(QuickLaunch) ;}
 }
 catch(err) { alert ("Loading failed");}
 placeholder.innerHTML = quicklaunchmenu.innerHTML;
}
Once you are done with the upload, copy the below mentioned code into to a content editor web part in SharePoint 2007 or HTML Form web part in SharePoint 2010. Make sure to update site URL and JS file path.


<iframe id="SourcePage" onload="DisplayQuickLaunch()" src="http://[Your Site]/default.aspx" style="display: none;"></iframe>

<script src="https//[Your JS file path]/DisplayQuickLaunch.js" type="text/javascript">
</script>
Hope this helps.

2 comments:

Sandesh said...

Hi,
I am not able to see any the quick launch bar, and it still appears blank, any suggestions?

Kolan Veera Reddy said...

Have you replaced your site URL in the iframe tag? Are you facing any JavaScript error? You can find it in left bootom of the browser.