0

I have created a site programmatically and I want to configure its current (left) navigation.

My code is:

public static void SetCurrentNavigation(SPWeb currentWeb)
{
    if (currentWeb != null)
    {
        if (currentWeb.Configuration == 2)
        {
            BreakInheritenceForCurrentNavigation(currentWeb);
            AddQuickLaunchItem("Lists", ListNames.EventsTitle, currentWeb.Url + "/Lists/" + ListNames.Events, currentWeb);
            AddQuickLaunchItem("Lists", ListNames.DiscussionTitle, currentWeb.Url + "/Lists/" + ListNames.DepartmentDiscussionUrl, currentWeb);
            AddQuickLaunchItem("Lists", ListNames.DepartmentTasksTitle, currentWeb.Url + "/Lists/" + ListNames.DepartmentTasks, currentWeb);
        }       
        currentWeb.Update();
    }
}
public static void BreakInheritenceForCurrentNavigation(SPWeb currentWeb)
{
    PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(currentWeb);
    currentWeb.AllowUnsafeUpdates = true;
    WebNavigationSettings webNavSettings = new WebNavigationSettings(currentWeb);
    pubWeb.Navigation.InheritCurrent = false;
    webNavSettings.CurrentNavigation.Source = StandardNavigationSource.PortalProvider;
    pubWeb.Navigation.ShowSiblings = false;
    DeleteRecentTag(currentWeb);
    pubWeb.Update();
}
public static void DeleteRecentTag(SPWeb currWeb)
{
    using (SPWeb web = currWeb)
    {
        web.AllowUnsafeUpdates = true;
        SPNavigationNodeCollection quickLaunch = web.Navigation.QuickLaunch;

        SPNavigationNode nodeHeader = quickLaunch.Cast<SPNavigationNode>().Where(n => n.Title == "Recent").FirstOrDefault();
        if (nodeHeader != null)
        {
            quickLaunch.Delete(nodeHeader);
        }
        web.Update();
    }
}

I debugged the whole program and its running fine but I am not getting the navigation affected i.e. the inheritance gets break but the lists are not displayed.

After site created I checked the navigation, in that I found no options are selected in current navigation and the labels and headers are added.

Preview is as below:

enter image description here

enter image description here

I found that I need to What am I missing?

0

You could use the following code to add a list or page to the quicklaunch

 private static void AddListToQuicklaunch(SPWeb web, string listTitle)
 {
    var list = web.Lists.TryGetList(listTitle);
    if (list != null)
    {
      list.OnQuickLaunch = true;
      list.Update();
    }
 }

or when adding a page

 private static void AddPageToQuicklaunch(SPWeb web, string pageUrl, string nodeTitle)
 {
   var newNode = new SPNavigationNode(nodeTitle, pageUrl, false);
   newNode = web.Navigation.QuickLaunch.AddAsLast(newNode);
   newNode.Update();
 }
  • But I made console application and used this and it works. But it is not working in my solution. – users1100 Dec 3 '13 at 9:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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