This is my code to delete the QuickLaunch Navigation Nodes. I am using the SharePoint 2013.
public void 2013NodeOrders(string siteUrl, string quickLaunch)
{
SPSecurity.RunWithElevatedPrivileges(() =>
{
using (var localWeb = new SPSite(siteUrl).OpenWeb())
{
try
{
var ql = localWeb.Navigation.QuickLaunch;
int count = 0;
while (count++ < 10)
{
try
{
CleanUpAutoCreatedNodes(ql, nodeNames.ToArray());
break;
}
catch (Exception ex)
{}
}
localWeb.Update();
}
catch (Exception ex)
{
Console.WriteLine("Error Occoured ", ex);
throw ex;
}
}
});
}
private static void CleanUpAutoCreatedNodes(SPNavigationNodeCollection ql, string[] nodenames)
{
foreach (SPNavigationNode globalNode in ql)
{
if (Array.IndexOf(nodenames, globalNode.Title) == -1)
{
if (globalNode.Children.Count > 0)
{
foreach (SPNavigationNode childNode in globalNode.Children)
childNode.Delete();
}
if (!globalNode.Title.Equals("Site Contents"))
globalNode.Delete();
}
}
}
I got the following COM Exception Error from the lines foreach (SPNavigationNode globalNode in ql) and foreach (SPNavigationNode childNode in globalNode.Children)
Error Message: Cannot complete this action. Please try again.0x80004005
Stacktrace Error
at Microsoft.SharePoint.Library.SPRequestInternalClass.GetNavigationNodeChild(String bstrUrl, Int32 lParentId, Int32 lIndex, Int32& lNodeId, String& pbstrNodeUrl, String& pbstrName, String& pbstrNameResource, String& pbstrDateModified, Int32& pbIsVisible, Int32& pbIsExternal, Int32& plNumChildren, Guid& pgScopeId, Int32& plParentObjectType)
at Microsoft.SharePoint.Library.SPRequest.GetNavigationNodeChild(String bstrUrl, Int32 lParentId, Int32 lIndex, Int32& lNodeId, String& pbstrNodeUrl, String& pbstrName, String& pbstrNameResource, String& pbstrDateModified, Int32& pbIsVisible, Int32& pbIsExternal, Int32& plNumChildren, Guid& pgScopeId, Int32& plParentObjectType)
Children
collection. Try stepping through to see if this is a specific node, or if this is a symptom of you deleting nodes from the collection. Also, see: stackoverflow.com/questions/10103225/…