views:

250

answers:

2

Hi,

I have a requirement to remove a number of the default nodes (i.e. People and Groups, Sites) in the left hand current navigation bar using the SharePoint API. Can anyone give me any guidance on how to achieve this?

Thanks, MagicAndi

+1  A: 

Your code will look something like this:

using (SPSite oSite= new SPSite("http://someurl/")){
    using (SPWeb oWeb = oSite.OpenWeb()){
        foreach (SPNavigationNode oNode in oWeb.Navigation.QuickLaunch)
        {
            if (oNode.Title == "Sites") {
                oNode.Delete();
            }
        }    
    }
}

be aware, though, that finding the item by title is not very recommended - it will differ if the web'b locale is not English. So it would be a better idea to find the node by its ID. See IDs here - http://msdn.microsoft.com/en-us/library/dd587301(office.11).aspx

naivists
naivists, thanks, looks promising. +1
MagicAndi
naivists, accepted as answer.
MagicAndi
+1  A: 

Based on naivists's answer:

public static void DeleteNavigationNodes(string p_sSiteUrl)
{
    try
    {
        SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            using (SPSite site = new SPSite(p_sSiteUrl))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    PublishingWeb pubWeb = null;
                    if (PublishingWeb.IsPublishingWeb(web))
                    {
                        pubWeb = PublishingWeb.GetPublishingWeb(web);

                        foreach (SPNavigationNode node in pubWeb.CurrentNavigationNodes)
                        {
                            if ((node.Id != 1003 ) && (node.Id != 1004 ))
                            {
                                node.Delete();
                            }
                        }

                        pubWeb.Update();           
                    }
                }
            }
        });
    }
    catch (Exception ex)
    {
        // Log error
     }
}

This article was also useful:

MagicAndi