3

I have a solution that deploys a web part with a web page, and I would like it to also add a Quick Launch home screen link to the created web page.

enter image description here

I tried a little code sniplet, that I added to my Feature Activation event listener, but it's not working.

SPSecurity.RunWithElevatedPrivileges(delegate()
{
 using (SPSite oSiteCollection = new SPSite("http://mySiteUrl"))
 {
       using (SPWeb oWeb = oSiteCollection.OpenWeb())
       {
             oWeb.AllowUnsafeUpdates = true;
             // Create the node.
             SPNavigationNodeCollection _quickLaunchNav = oWeb.Navigation.QuickLaunch;

             SPNavigationNode _SPNode = new SPNavigationNode("Link Title", "http://link_url", true); //i tried with false, too. It didn't work

             _quickLaunchNav.AddAsLast(_SPNode);
             oWeb.Update(); //added this since I've seen it in some examples, but not every example had it
       }
 }
});

Am I missing something really basic in this?

I even tried the method described here, but it was not doing anything: Best way to programmatically create/maintain SharePoint Quick Launch menu

4 Answers 4

1

Use _quickLaunchNav.Parent.Children.AddAsLast(_SPNode);

and try using SPNavigationNode constructor with 2 arguments instead of 3.

Hope this helps.

1
  • Hi! Thanks for the reply, but the edits did not help. It runs the code, I don't get an error, or anything, but it's just simply not added.
    – Laureant
    Mar 18, 2015 at 15:44
1

Your code which adds links to the navigations seems to be correct.

Maybe you have a general problem with your code...

You are in the event receiver and can use your reference to the site. No need to initialize a new SPSite instance or do something with elevated privileges.

Try this:

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    using (SPSite site = (SPSite)properties.Feature.Parent)
    {
        using (SPWeb web = site.RootWeb) { 
        {           
            // Add links to navigation
            SPNavigationNode listNode = new SPNavigationNode("Visitors", "Lists/Visitors/All Visitors.aspx");
            SPNavigationNode newVisitorNode = new SPNavigationNode("Register New Visitor", "Lists/Visitors/NewForm.aspx");
            SPNavigationNode siteContentsNode = new SPNavigationNode("Site Contents", "_layouts/15/viewlsts.aspx");

            web.Navigation.QuickLaunch.AddAsLast(listNode);
            web.Navigation.QuickLaunch.AddAsLast(newVisitorNode);
            web.Navigation.QuickLaunch.AddAsLast(siteContentsNode);

            web.Update();
        }
    }
}
2
  • using (SPWeb web = (SPWeb)properties.Feature.Parent) - this line is giving me a casting error (Cannot Cast from SPSite to SPWeb) Instead of that I'm using the SPWeb web = properties.GetWeb(); But no luck with it, whatsoever.
    – Laureant
    Mar 19, 2015 at 8:00
  • Oh, then your feature has the scope "Site", so the parent is a SPSite, not a SPWeb. I modified the code, should work now. Mar 19, 2015 at 8:22
1

Add link to Sharepoint Quick Launch programmatically :

// references
using Microsoft.SharePoint.Publishing;
using Microsoft.SharePoint.Navigation;
using Microsoft.SharePoint.Publishing.Navigation;

// code to add Navigation link
using (SPSite spSite = SPContext.Current.Site)
    {
        using (SPWeb web = spSite.OpenWeb())
            {                                                
                        string headingTitle = "myPageTitle";
                        string headingUrl = "http://mysitename.com/SitePage/myPage.aspx";

                        // 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);
                        }                                         
            }
     }
1

Try this code:

SPSite spSite = (SPSite)properties.Feature.Parent
using (SPWeb web = spSite.OpenWeb())
{
    SPNavigationNode listNode = new SPNavigationNode("Visitors", "Lists/Visitors/All Visitors.aspx");
    SPNavigationNode newVisitorNode = new SPNavigationNode("Register New Visitor", "Lists/Visitors/NewForm.aspx");
    SPNavigationNode siteContentsNode = new SPNavigationNode("Site Contents", "_layouts/15/viewlsts.aspx");
    SPNavigationNodeCollection nodes = web.Navigation.QuickLaunch;
    nodes.AddAsLast(listNode);
    nodes.AddAsLast(newVisitorNode);
    nodes.AddAsLast(siteContentsNode);
    web.Update();
}

Your Answer

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

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