I would like to delete all the links in a quicklaunch in SP2010 with C#. I tought this will do it, but somehow they are not gonna deleted:
SPNavigationNodeCollection n = subSite.Navigation.QuickLaunch;
foreach (SPNavigationNode node in n)
{
node.Delete();
}
Im able to add links, but not to delete them :/ Any ideas? Thx
Edit: Ah got the fix :)
I cannot write just node.Delete()
I need to write n.Delete(node)
Edit2: hm somehow I dont get deleted ALL the links. If I run the code 2-3 Times then all of them are deleted, weird
Try this code:
SPNavigationNodeCollection nodes = web.Navigation.QuickLaunch;
for(int i = nodes.Count - 1; i >= 0; i--)
{
nodes[i].Delete();
}
see this link also: http://www.mtelligent.com/journal/2007/10/7/customizing-the-quick-launch-menu-with-spnavigationnode-spna.html
I have experienced this same problem myself.
I was able to delete the links on the quick launch by deleting from the bottom up. My theory is that the collection is shifted up after deleting a link on the quick launch, so when link[0] gets deleted, link[1] becomes link[0]. Then when attempting to delete link[1], link[2] actually gets deleted.
Your problem is that at some point link[i] does not exist and error 'cannot complete this action' is thrown, yet you have not deleted all the links.
//Iterate from the bottom of the links to the top
for (i = numLinks-1; i >=0 ; i--)
{
links[i].Delete();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With