Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Hi I've put this question up here : http://stackoverflow.com/questions/13130785/delete-navigation-node-with-powershell-from-sharepoint-quicklaunch-in-all-sites but no answer yet so posting here too.

I've put together a powershell script to delete a specific quicklaunch node in sharepoint 2010. It works on a dummy site collection, but fails on another (migrated sites from sp2007).

Here is the script:

Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue 
$site = Get-SPSite http://site.com/SITES/OFFICES/
$site | get-spweb -limit all | foreach-object{ 
$webURL = $_.url 
$web = Get-SPWeb $webURL 

$navigationNodes = $web.Navigation.QuickLaunch 
ForEach ($Node in $NavigationNodes) 
{ 
 if($node.Title -eq "People and Group") 
    { 
    write-host $web.URL "...node exists here"   
    {continue}  
    $node.Delete()
    $web.update() 
    write-host $web.URL "...node removed"    
    } 
} 

 $web.Dispose() 
 }

The error I get when running as SP_Admin is :

Get-SPWeb : The pipeline has been stopped. At line:3 char:18 + $site | Get-SPWeb <<<< -limit all | foreach-object { + CategoryInfo : InvalidData: (Microsoft.Share....SPCmdletGetWeb:SPCmdletGetWeb) [Get-SPWeb], PipelineStoppedException + FullyQualifiedErrorId : Microsoft.SharePoint.PowerShell.SPCmdletGetWeb

ForEach-Object : Cannot complete this action.

Please try again. At line:3 char:46 + $site | Get-SPWeb -limit all | foreach-object <<<< { + CategoryInfo : NotSpecified: (:) [ForEach-Object], SPException + FullyQualifiedErrorId : Microsoft.SharePoint.SPException,Microsoft.PowerShell.Commands.ForEachObjectCommand

I am new to powershell so trying to debug the issue - do I need to build an array of the nodes before deleting them?

Any guidance would be great. Thanks Rich

share|improve this question

1 Answer 1

You need to delete the items in reverse order. this is because you cannot modify the collection inside the foreach loop.

Do the below:

SPNavigationNodeCollection nodes = myPublishingWeb.Navigation.CurrentNavigationNodes;

                    //deleting unwanted
                    int nodesCount = nodes.Count;
                    for (int nodeIndex = nodesCount; nodeIndex > 0; nodeIndex--)
                    {
                        SPNavigationNode spNodes = nodes[nodeIndex - 1];

                        foreach (string unWantedNode in unWantedNodes)
                        {
                            if (spNodes.Title.ToString() == unWantedNode)
                            {
                                spNodes.Delete();
                            }
                        }
                    }

myPublishingWeb.Update();
                    webSite.Update();

UPDATE: unWantedNode is just an array of strings. You loop through existing SP nodes & if there is a match with any of your required nodes, you proceed with delete.

I do not have exact code, but I can give algorithm as below:

string[] unWantedNodes = {"Complaince"};
SPNavigationNodeCollection nodes = myPublishingWeb.Navigation.CurrentNavigationNodes;

                    //deleting unwanted
                    int nodesCount = nodes.Count;
                    for (int nodeIndex = nodesCount; nodeIndex > 0; nodeIndex--)
                    {
                        SPNavigationNode spNodes = nodes[nodeIndex - 1];

                        foreach (string unWantedNode in unWantedNodes)
                        {
                            if (spNodes.Title.ToString() == unWantedNode)
                            {
                               //HERE again get the list of children using spNodes.Childre... & then do the same deleting logic for child nodes
                               spNodes.Update();
                                spNodes.Delete();

                            }
                        }
                    }

myPublishingWeb.Update();
                    webSite.Update();
share|improve this answer
    
Hi What is unWantedNode? could you show how to delete a heading called Compliance and all children within? Thanks –  Raj Chahal Feb 2 '13 at 11:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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