Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. SPSecurity.RunWithElevatedPrivileges(delegate()
  2. {
  3. using (SPSite oSiteCollection = new SPSite("http://mySiteUrl"))
  4. {
  5. using (SPWeb oWeb = oSiteCollection.OpenWeb())
  6. {
  7. oWeb.AllowUnsafeUpdates = true;
  8. // Create the node.
  9. SPNavigationNodeCollection _quickLaunchNav = oWeb.Navigation.QuickLaunch;
  10.  
  11. SPNavigationNode _SPNode = new SPNavigationNode("Link Title", "http://link_url", true); //i tried with false, too. It didn't work
  12.  
  13. _quickLaunchNav.AddAsLast(_SPNode);
  14. oWeb.Update(); //added this since I've seen it in some examples, but not every example had it
  15. }
  16. }
  17. });
  18.  
  19. public override void FeatureActivated(SPFeatureReceiverProperties properties)
  20. {
  21. using (SPSite site = (SPSite)properties.Feature.Parent)
  22. {
  23. using (SPWeb web = site.RootWeb) {
  24. {
  25. // Add links to navigation
  26. SPNavigationNode listNode = new SPNavigationNode("Visitors", "Lists/Visitors/All Visitors.aspx");
  27. SPNavigationNode newVisitorNode = new SPNavigationNode("Register New Visitor", "Lists/Visitors/NewForm.aspx");
  28. SPNavigationNode siteContentsNode = new SPNavigationNode("Site Contents", "_layouts/15/viewlsts.aspx");
  29.  
  30. web.Navigation.QuickLaunch.AddAsLast(listNode);
  31. web.Navigation.QuickLaunch.AddAsLast(newVisitorNode);
  32. web.Navigation.QuickLaunch.AddAsLast(siteContentsNode);
  33.  
  34. web.Update();
  35. }
  36. }
  37. }
  38.  
  39. // references
  40. using Microsoft.SharePoint.Publishing;
  41. using Microsoft.SharePoint.Navigation;
  42. using Microsoft.SharePoint.Publishing.Navigation;
  43.  
  44. // code to add Navigation link
  45. using (SPSite spSite = SPContext.Current.Site)
  46. {
  47. using (SPWeb web = spSite.OpenWeb())
  48. {
  49. string headingTitle = "myPageTitle";
  50. string headingUrl = "http://mysitename.com/SitePage/myPage.aspx";
  51.  
  52. // Get the Quick Launch headings.
  53. SPNavigationNodeCollection ql = web.Navigation.QuickLaunch;
  54.  
  55. // If a Resources heading exists, get it.
  56. SPNavigationNode heading = ql.Cast<SPNavigationNode>().FirstOrDefault(n => n.Title == headingTitle);
  57.  
  58. // If the Resources heading does not exist, create it.
  59. if (heading == null)
  60. {
  61. heading = new SPNavigationNode(headingTitle, headingUrl);
  62. heading = ql.AddAsLast(heading);
  63. }
  64. }
  65. }
  66.  
  67. SPSite spSite = (SPSite)properties.Feature.Parent
  68. using (SPWeb web = spSite.OpenWeb())
  69. {
  70. SPNavigationNode listNode = new SPNavigationNode("Visitors", "Lists/Visitors/All Visitors.aspx");
  71. SPNavigationNode newVisitorNode = new SPNavigationNode("Register New Visitor", "Lists/Visitors/NewForm.aspx");
  72. SPNavigationNode siteContentsNode = new SPNavigationNode("Site Contents", "_layouts/15/viewlsts.aspx");
  73. SPNavigationNodeCollection nodes = web.Navigation.QuickLaunch;
  74. nodes.AddAsLast(listNode);
  75. nodes.AddAsLast(newVisitorNode);
  76. nodes.AddAsLast(siteContentsNode);
  77. web.Update();
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement