Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete all links from quicklaunch?

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

like image 316
sabisabi Avatar asked Dec 02 '22 00:12

sabisabi


2 Answers

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

like image 180
Rony SP Avatar answered Dec 05 '22 05:12

Rony SP


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();
}
like image 22
ClairePoint Avatar answered Dec 05 '22 05:12

ClairePoint