0

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.

2 Answers 2

1

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.

2

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.

4
  • Gettting following error for the cast. 'Microsoft.SharePoint.Navigation.SPNavigationNodeCollection' does not contain a definition for 'Cast' and no extension method 'Cast' accepting a first argument of type 'Microsoft.SharePoint.Navigation.SPNavigationNodeCollection' could be found (are you missing a using directive or an assembly reference?) I just enumerated through the collection to get to the required SPNavigationNode.
    – bkk
    Commented Feb 26, 2015 at 8:45
  • You need to add these three references : using Microsoft.SharePoint.Navigation;, using Microsoft.SharePoint.Publishing.Navigation;, using Microsoft.SharePoint.Publishing; Commented Feb 26, 2015 at 10:14
  • Sorry, added these references but same result. FYI I m working SPO on Office 365.
    – bkk
    Commented Feb 26, 2015 at 11:50
  • It's working on Office 365 for me. If you are using 'Sharepoint Foundation' then it will not work, try on Sharepoint Server. If it will not working too, then activate SharePoint Server Publishing Infrastructure feature from 'Site Collection Features' & SharePoint Server Publishing feature from 'Site Features'. Commented Feb 27, 2015 at 4:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.