Add Link to the Quick Launch Navigation while deploying a webpart page in the module
-
02-10-2020 - |
Domanda
I like to add a navigation link under the quick launch left nav bar when a module deploys a webpart page as a file element in the feature. Is there a declarative way or do I go with feature event receiver?
The environment I m working on - Office 365 and deploying the webpart page under a feature using a Sandbox solution.
Soluzione
There is no declarative way to make the page deployed through File element to show up in the Quick Launch. Check the attributes of File Element here: https://msdn.microsoft.com/en-us/library/office/ms459213.aspx So, using the event receiver may be a right approach.
Altri suggerimenti
Add this code to your FeatureActivated
method of the Event Receiver:
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
using (SPWeb web = (properties.Feature.Parent as SPWeb))
{
web.AllowUnsafeUpdates = true;
string headingTitle = "myNavigation"; // Navigation Title
string headingUrl = "navigationURL"; // URL of the Navigation Link
// Get the Quick Launch headings.
SPNavigationNodeCollection ql = web.Navigation.QuickLaunch;
// If a Resources heading exists, get it.
SPNavigationNode heading = ql.Cast<SPNavigationNode>().FirstOrDefault(n => n.Title == headingTitle);
// If the Resources heading does not exist, create it.
if (heading == null)
{
heading = new SPNavigationNode(headingTitle, headingUrl);
heading = ql.AddAsLast(heading);
}
web.Update();
web.AllowUnsafeUpdates = false;
}
}
It will add the Navigation Link on feature activation.