Home > Quick Launch Control, SharePoint Products and Technology > Hide SharePoint QuickLaunch And Add Custom QuickLaunch

Hide SharePoint QuickLaunch And Add Custom QuickLaunch


So with reference to my previous post here I’m trying to explain my first coding tact of hiding existing quick launch and replace it with custom webpart that renders links in same fashion as default quick launch does.

Here in this the idea is such that,

our custom webpart will first search for the default quicklaunch control in the master page

set it’s visibility to false and then sets his(custom webpart) own visibility to true

Then start adding the links to the webpart simultaneously apply the cosmetic work on the links so that it looks like the default quick launch links.

To find the default quicklaunch control in the master page below code will be helpful:

protected override void CreateChildControls()
{
List<string> controlsList = new List<string>();
if (Page.Master.HasControls())
{
for (int controlIndex = 0; controlIndex < Page.Master.Controls.Count; controlIndex++)
{
controlsList.Add(“<br>”);
controlsList.Add(controlIndex.ToString() + “. ” + Page.Master.Controls[controlIndex].UniqueID);
controlsList.Add(“<br> Level 1: <br>”);
for (int childCntrlIndex = 0; childCntrlIndex < Page.Master.Controls[controlIndex].Controls.Count; childCntrlIndex++)
{
controlsList.Add(childCntrlIndex.ToString() + “. ” + Page.Master.Controls[controlIndex].Controls[childCntrlIndex].UniqueID);
controlsList.Add(“<br> Level 2: <br>”);

for (int innerChildCntrlIndex = 0; innerChildCntrlIndex < Page.Master.Controls[controlIndex].Controls[childCntrlIndex].Controls.Count; innerChildCntrlIndex++)
{

controlsList.Add(innerChildCntrlIndex.ToString() + “. ” + Page.Master.Controls[controlIndex].Controls[childCntrlIndex].Controls[innerChildCntrlIndex].UniqueID + “<br>”);
controlsList.Add(“<br> Level 3: <br>”);

for (int level3Index = 0; level3Index < Page.Master.Controls[controlIndex].Controls[childCntrlIndex].Controls[innerChildCntrlIndex].Controls.Count; level3Index++)
{
controlsList.Add(level3Index.ToString() + “. ” + Page.Master.Controls[controlIndex].Controls[childCntrlIndex].Controls[innerChildCntrlIndex].Controls[level3Index].UniqueID + “<br>”);

}

controlsList.Add(“-:Level 3 END:-<br>”);

}

controlsList.Add(“-:Level 2 END:-<br>”);

}

controlsList.Add(“<br> -:Level 1 END:- <br>”);

}
}
StringBuilder str = new StringBuilder();

for (int controlListIndex = 0; controlListIndex < controlsList.Count; controlListIndex++)

{

str.Append(controlsList[controlListIndex].ToString());

}

//HttpContext.Current.Response.WriteFile(“Pagecontroldetails.txt”, false);

HttpContext.Current.Response.Clear();

HttpContext.Current.Response.AddHeader(“content-disposition”, “attachment;filename=Pagecontroldetails.txt”);

HttpContext.Current.Response.Charset = “”;

HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);

HttpContext.Current.Response.ContentType = “application/vnd.text”;

System.IO.StringWriter stringWrite = new System.IO.StringWriter();

System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

HttpContext.Current.Response.Write(str.ToString());

HttpContext.Current.Response.End();

base.CreateChildControls();

}

The above code will help you get the exact controlid in the masterpage for quick launch control. If you don’t want to this all for getting the controlid then I will tell you the control ID 🙂 its “ctl00$PlaceHolderLeftNavBar$QuickLaunchNavigationManager

Hmm… Now comes the main task i.e is point no. 2 and 3

protected override void CreateChildControls()
{
Control masterPageControl = new Control();
try
{
QueryXmlDocumentForAuthorizingLoggedInUser();
AuthorizeLoggedInUser();
if (NonAdminUser)
{
masterPageControl = Page.Master.FindControl(“ctl00$PlaceHolderLeftNavBar$QuickLaunchNavigationManager”);
masterPageControl.Visible = false;
}
else
{   // unhide the quick launch control on the page and set pearlNavigation control to invisible
masterPageControl = Page.Master.FindControl(“ctl00$PlaceHolderLeftNavBar$QuickLaunchNavigationManager”);
masterPageControl.Visible = true;
}
}
catch (Exception ex)
{
EventLog pearlNavLog = new EventLog(“Application”, “.”);
pearlNavLog.WriteEntry(ex.Message, EventLogEntryType.FailureAudit);
}
base.CreateChildControls();
}

protected override void Render(HtmlTextWriter writer)
{
if (NonAdminUser)
{
// get details of links from xml file
QueryXmlDocumentForBuildingNavigationLinks();

// add the main html tag here
StringBuilder startingHTML = new StringBuilder();
startingHTML.Append(
//”<table height=’100%’ cellpadding=’0′ cellspacing=’0′ border=’0′>” + Environment.NewLine
//+”<tr valign=’top’>” + Environment.NewLine + “<td width=’4px’> <img src=’/_layouts/images/blank.gif’ width=’4′ height=’1′ alt=”> </td>” + Environment.NewLine
//+”<td valign=’top’ width=’100%’> <div class=’ms-quicklaunchouter’> <div class=’ms-quickLaunch’ style=’width: 100%’>” + Environment.NewLine
“<div id=’ctl00_PlaceHolderLeftNavBar_QuickLaunchNavigationManager’> <div>” + Environment.NewLine
+ “<table id=’zz2_QuickLaunchMenu’ class=’ms-navSubMenu1 zz2_QuickLaunchMenu_7 zz2_QuickLaunchMenu_2′ cellpadding=’0′ cellspacing=’0′ border=’0′>” + Environment.NewLine
);

StringBuilder mainHTML = new StringBuilder();
// Iterate the links collection here for adding the links

bool isChildPresent = false;

for (int linkIndex = 0; linkIndex < QuickNavigationLinksCollection.Count; linkIndex++)
{
if (string.IsNullOrEmpty(QuickNavigationLinksCollection[linkIndex].Header))
{// Header tag here
if (isChildPresent)
{ // Last part of child
mainHTML.Append(“</table>” + Environment.NewLine + “</td>” + Environment.NewLine + “</tr>”);
}
isChildPresent = false;
mainHTML.Append(“<tr id=’zz2_QuickLaunchMenun” + linkIndex + “‘ >”);
mainHTML.Append(Environment.NewLine);
mainHTML.Append(“<td>” + Environment.NewLine + “<table class=’ms-navheader zz2_QuickLaunchMenu_4′ cellpadding=’0′ cellspacing=’0′ border=’0′ width=’100%’>”);
mainHTML.Append(Environment.NewLine);
mainHTML.Append(“<tr>” + Environment.NewLine + “<td style=’width: 100%;’>”);
mainHTML.Append(Environment.NewLine);
mainHTML.Append(“<a class=’zz2_QuickLaunchMenu_1 ms-navheader zz2_QuickLaunchMenu_3′ style=’border-style: none; font-size: 1em;’>” + QuickNavigationLinksCollection[linkIndex].Title + “</a>”);
mainHTML.Append(Environment.NewLine);
mainHTML.Append(“</td>” + Environment.NewLine + “</tr>” + Environment.NewLine + “</table>” + Environment.NewLine + “</td>” + Environment.NewLine + “</tr>”);
mainHTML.Append(Environment.NewLine);

}
else
{
if (!isChildPresent)
{  // First part of child
mainHTML.Append(“<tr>” + Environment.NewLine + “<td>” + Environment.NewLine + “<table border=’0′ cellpadding=’0′ cellspacing=’0′ width=’100%’ class=’ms-navSubMenu2 zz2_QuickLaunchMenu_8′>”);
}

isChildPresent = true;
//mainHTML.Append(“<tr>” + Environment.NewLine + “<td>” + Environment.NewLine + “<table border=’0′ cellpadding=’0′ cellspacing=’0′ width=’100%’ class=’ms-navSubMenu2 zz2_QuickLaunchMenu_8′>”);
//mainHTML.Append(Environment.NewLine);
mainHTML.Append(“<tr onmouseover=’Menu_HoverStatic(this)’ onmouseout=’Menu_Unhover(this)’ onkeyup=’Menu_Key(event)’ id=’zz2_QuickLaunchMenun” + linkIndex + “‘>”);
mainHTML.Append(Environment.NewLine);
mainHTML.Append(“<td>” + Environment.NewLine + “<table class=’ms-navitem zz2_QuickLaunchMenu_6′ cellpadding=’0′ cellspacing=’0′ border=’0′ width=’100%’>”);
mainHTML.Append(Environment.NewLine);
mainHTML.Append(“<tr>” + Environment.NewLine + “<td style=’width: 100%;’> “);
mainHTML.Append(Environment.NewLine);
mainHTML.Append(“<a class=’zz2_QuickLaunchMenu_1 ms-navitem zz2_QuickLaunchMenu_5′ href='” + QuickNavigationLinksCollection[linkIndex].URL
+ “‘ style=’border-style: none; font-size: 1em;’>” + QuickNavigationLinksCollection[linkIndex].Title + “</a>”);
mainHTML.Append(Environment.NewLine);
mainHTML.Append(” </td>” + Environment.NewLine + “</tr>” + Environment.NewLine + “</table>” + Environment.NewLine + “</td>” + Environment.NewLine + “</tr>”);
//mainHTML.Append(Environment.NewLine + “</table>” + Environment.NewLine + “</td> </tr>”);
mainHTML.Append(Environment.NewLine);
}
}

if (isChildPresent)
{ // Last part of child
mainHTML.Append(“</table>” + Environment.NewLine + “</td>” + Environment.NewLine + “</tr>”);
}

StringBuilder endingHTML = new StringBuilder();
endingHTML.Append(“</table>” + Environment.NewLine + “</div>” + Environment.NewLine + “</div>”); // +Environment.NewLine + “</div>” + Environment.NewLine + “</div>” + Environment.NewLine + “</td>” + Environment.NewLine + “</tr>” + Environment.NewLine + “<tr>” + Environment.NewLine + “<td colspan=’2′>” + Environment.NewLine + “<img src=’/_layouts/images/blank.gif’ width=’138′ height=’1′ alt=”>” + Environment.NewLine + “</td>” + Environment.NewLine + “</tr>” + Environment.NewLine + “</table>”);

// Now write the complete html output
writer.Write(startingHTML.ToString());
writer.Write(mainHTML.ToString());
writer.Write(endingHTML.ToString());
}
base.Render(writer);
}
}

That’s it and you are done with the work

Advertisements
  1. November 6, 2010 at 5:22 pm

    Are you a Sharepoint developer? If Yes, then why not Sell your webparts and earn some extra money.
    visit Submit Your WebPart

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: