0

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)
2
  • First off, you can't modify a collection while iterating through it, so you need to use an index into the collection. The exception you're getting is an internal error from SharePoint trying to create the 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/… Feb 18, 2014 at 5:45
  • Also, you get a reference to the collection once, outside of your loop, and then try to process it 10 times(?!?) and never refresh your in memory collection. You may be deleting nodes and then trying to retrieve nodes that no longer exist. Feb 18, 2014 at 5:48

1 Answer 1

1

You can use this code to delete all the nodes:

SPNavigationNodeCollection nodes = web.Navigation.QuickLaunch;
 for(int i = nodes.Count - 1; i >= 0; i--)
            {
                nodes[i].Delete();
            }
1
  • the above exception was solve by the code you have suggested. but still i am having same problem for the method below. private int FindPositionInTarget(string p, SPNavigationNodeCollection ql) { for (int i = ql.Count - 1; i >= 0; i--) if (ql[i].Title == p) return i; return -1; }
    – Binod
    Feb 20, 2014 at 5:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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