0

I created a custom sitemap provider but I am unable to make it show the next level.

Here is my first attempt:

public override SiteMapNodeCollection GetChildNodes(SiteMapNode node)
{
        var collection = new SiteMapNodeCollection();
        var pNode = node as PortalSiteMapNode;

        if (node.ParentNode == null)
        {
            PortalSiteMapNode global = new PortalSiteMapNode(pNode.WebNode, "1", NodeTypes.Area, "http://sharepoint2013/", "1", "1");
            global.ParentNode = node;
            collection.Add(global);
        }
        else if (node.Key == "1")
        {
            PortalSiteMapNode global2 = new PortalSiteMapNode((node.ParentNode as PortalSiteMapNode).WebNode, "1.1", NodeTypes.Area, "http://sharepoint2013/level1", "1.1", "1.1");
            global2.ParentNode = node;
            collection.Add(global2);
        }
        return collection;
}

Here is my second attempt (recursive):

public override SiteMapNodeCollection GetChildNodes(SiteMapNode node)
        {
            var collection = new SiteMapNodeCollection();
            var pNode = node as PortalSiteMapNode;

            if (node.ParentNode == null)
            {
                PortalSiteMapNode level1 = new PortalSiteMapNode(pNode.WebNode, "1", NodeTypes.Area, "http://sharepoint2013/", "1", "1");
                level1.ParentNode = node;
                collection.Add(level1);

                PortalSiteMapNode level2 = new PortalSiteMapNode(level1.WebNode, "1.1", NodeTypes.Area, "http://sharepoint2013/", "1.1", "1.1");
                level1.ChildNodes.Add(level2);
            }


            return collection;
        }

Here is the Delegate Control XML:

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Control Sequence="30"
           Id="QuickLaunchDataSource"
           ControlClass="Microsoft.SharePoint.Publishing.Navigation.PortalSiteMapDataSourceSwitch"
           ControlAssembly="Microsoft.SharePoint.Publishing, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c">
    <Property Name="ID">QuickLaunchSiteMap</Property>
    <Property Name="SiteMapProvider">MyQuicklaunchProvider</Property>
    <Property Name="EnableViewState">false</Property>
    <Property Name="StartFromCurrentNode">false</Property>
    <Property Name="ShowStartingNode">false</Property>
    <Property Name="TreatStartingNodeAsCurrent">false</Property>
    <Property Name="TrimNonCurrentTypes">Heading</Property>
  </Control>
</Elements>

Result is always this:

enter image description here

Am I missing the obvious here? It cant be that hard...

Update 1: I want 3 or more levels created entirely with code. They must be visible on every page.

0

You have to set "ShowStartingNode=true" in the delegate control properties of the custom sitemap provider. Setting it to false makes it to skip the rootnode in the sitemap hierarchy.

  • I dont want the Root node to be visible. – Max Melcher Aug 30 '13 at 11:27
  • 1
    You can hide the root node through simple css, Instead of rewriting your code to form a hierarchy without root node. My answer is not wrong to be down voted, this fix will save a lot of time and effort! – msounthar Aug 30 '13 at 11:38
  • Sorry this fix is not related to my problem - i want 4 or more levels visible but thats not possiblevwith the OOTB control. – Max Melcher Aug 31 '13 at 7:38
0
public override SiteMapNodeCollection GetChildNodes(System.Web.SiteMapNode node)
{
    PortalSiteMapNode portalNode = node as PortalSiteMapNode;
    if (portalNode != null)
    {
       if (portalNode.Type == NodeTypes.Area)
       {
           SiteMapNodeCollection nodeColl = base.GetChildNodes(portalNode);
           SPWeb currentWeb = SPContext.Current.Web;
           PortalSiteMapNode level1 = new PortalSiteMapNode(pNode.WebNode, "1", NodeTypes.Area, "http://sharepoint2013/", "1", "1");


           nodeColl.Add(level1);

           for (int i = 0; i < 20; i++)
           {
                PortalSiteMapNode level2 = new PortalSiteMapNode(level1.WebNode, "1.1" + i, NodeTypes.Area, "http://sharepoint2013/", "1.1", "1.1");
                level1.ChildNodes.Add(level2);
           }
           return nodeColl;
       }
       else
       {
       return base.GetChildNodes(portalNode);
   }
   else
   {
      return new SiteMapNodeCollection();
   }
}

http://social.msdn.microsoft.com/Forums/sharepoint/fr-FR/734cfd61-cce8-4adb-9d4f-d77bf42f815e/inserting-a-portalsitemapnode-on-existing-sitemapnodecollection-creates-duplicates

within the masterpage you need somthing like this:

<sharepoint:aspmenuid=”topnavigationmenu” runat=”server” datasourceid=”topSiteMap1″ enableviewstate=”false” accesskey=”">” Orientation=”Horizontal” StaticDisplayLevels=”3″ MaximumDynamicDisplayLevels=”3″ DynamicHorizontalOffset=”0″ StaticPopoutImageUrl=”/_layouts/images/menudark.gif” StaticPopoutImageTextFormatString=”" DynamicHoverStyle-BackColor=”#CBE3F0″ SkipLinkText=”" StaticSubMenuIndent=”0″ CssClass=”ms-topNavContainer”> 

<staticmenustyle/><staticmenuitemstyle cssclass=”ms-topnav” itemspacing=”0px”/><staticselectedstyle cssclass=”ms-topnavselected”/><statichoverstyle cssclass=”ms-topNavHover”/><dynamicmenustyle backcolor=”#F2F3F4″ bordercolor=”#A7B4CE” borderwidth=”1px”/><dynamicmenuitemstyle cssclass=”ms-topNavFlyOuts”>/><dynamicselectedstyle cssclass=”ms-topNavFlyOutsSelected”/></SharePoint:AspMenu> 
<asp:sitemapdatasource showstartingnode=”False” sitemapprovider=”MyCustomNavigationProvider” id=”topSiteMap1″ runat=”server” startfromcurrentnode=”true”> 

http://msdn.microsoft.com/en-us/library/ms466994(v=office.14).aspx

and

add this:

ShowStartingNode="False" its needs to be false otherwise by default its set to true

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sitemapdatasource.showstartingnode.aspx

EDIT

as the code above doenst work (made without testing ;) my bad) and i dont specifically know the error and what line is causing it, you can use this instead:

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint.Publishing;
using Microsoft.SharePoint.Publishing.Navigation;
using System.Web;
using System.Web.UI.WebControls;
using System.Configuration;

namespace MyCustomNav
{
public class Navigation: PortalSiteMapProvider
{
public override SiteMapNodeCollection GetChildNodes(System.Web.SiteMapNode 
node)
{
PortalSiteMapNode pNode = node as PortalSiteMapNode;
if (pNode != null)
{
if (pNode.Type == NodeTypes.Area)
{
SiteMapNodeCollection nodeColl = base.GetChildNodes(pNode);
PortalSiteMapNode childNode= new PortalSiteMapNode(pNode.WebNode, "1", NodeTypes.Area, "http://sharepoint2013/", "1", "1");

PortalSiteMapNode childNode1 = new PortalSiteMapNode(level1.WebNode, "1.1" + i, NodeTypes.Area, "http://sharepoint2013/", "1.1", "1.1");

nodeColl.Add(childNode);

SiteMapNodeCollection test = new SiteMapNodeCollection();
test.Add(childNode1);
childNode.ChildNodes = test;

return nodeColl;
}
else
return base.GetChildNodes(pNode);
}
else
return new SiteMapNodeCollection();
}
}
}

add this to the web.config

<add name="MyCustomNavigationProvider" type="MyCustomNav.Navigation, MyCustomNav" 
NavigationType="Current" />

add this to the masterpage under ContentPlaceHolder

<SharePoint:AspMenu
ID="TopNavigationMenu"
  Runat="server"
  DataSourceID="topSiteMap1"
  EnableViewState="false"
  AccessKey="<%$Resources:wss,navigation_accesskey%>"
  Orientation="Horizontal"
  StaticDisplayLevels="1"
  MaximumDynamicDisplayLevels="3"
  DynamicHorizontalOffset="0"
  StaticPopoutImageUrl="/_layouts/images/menudark.gif"
  StaticPopoutImageTextFormatString=""
  DynamicHoverStyle-BackColor="#CBE3F0"
  SkipLinkText=""
  StaticSubMenuIndent="0"
  CssClass="ms-topNavContainer">
<StaticMenuStyle/>
<StaticMenuItemStyle CssClass="ms-topnav" ItemSpacing="0px"/>
<StaticSelectedStyle CssClass="ms-topnavselected" />
<StaticHoverStyle CssClass="ms-topNavHover" />
<DynamicMenuStyle BackColor="#F2F3F4" BorderColor="#A7B4CE" 
  BorderWidth="1px"/>
<DynamicMenuItemStyle CssClass="ms-topNavFlyOuts"/>
<DynamicHoverStyle CssClass="ms-topNavFlyOutsHover"/>
<DynamicSelectedStyle CssClass="ms-topNavFlyOutsSelected"/>
</SharePoint:AspMenu>
<asp:SiteMapDataSource
  ShowStartingNode="False"
  SiteMapProvider="MyCustomNavigationProvider"
  id="topSiteMap1"
  runat="server"
  StartFromCurrentNode="true"/>

ammend the code to suit your needs!

http://msdn.microsoft.com/en-us/library/cc789625(v=office.14).aspx

  • Your code goes straight in a stackoverflow exception. Please see my update. – Max Melcher Aug 30 '13 at 11:26
  • just changed my answer! refer to the edit section for the updated code ;) – Ali Jafer Aug 30 '13 at 12:13
0

okay, its not possible with the PortalSitemapProvider. Its HARDCODED.

enter image description here

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.