multiple SiteMap nodes with same URL with querystring parameters

1.9k Views Asked by At

in the SiteMap control i am using, I want 2 or more of my nodes to point to the same page.

 <siteMapNode url="~/Default.aspx" title="Home"  description="Home page">
  <siteMapNode url="~/SectionList.aspx" title="By section"  description="List of sections">
    <siteMapNode url="~/BrowsePublications.aspx" title="publications of section"  description="Publications of section"/>
  </siteMapNode>
  <siteMapNode url="~/Officers.aspx" title="By responsible officer"  description="List of officers">
    <siteMapNode url="~/BrowsePublications.aspx" title="publications of officer"  description="Publications of officer"/>
  </siteMapNode>
</siteMapNode>

I basically have a grid to show, this grid can be filtered in many ways. Each filter is a page where the user clicks a certain entry and gets redirected to the page showing the grid with data relevant to that entry. I am using query string parameters to achieve that. I read online that one way to overcome this limitation is to append dummy parameters or a '#' to the end of the urls of the duplicate nodes, so i tried with this

 <siteMapNode url="~/Default.aspx" title="Home"  description="Home page">
  <siteMapNode url="~/SectionList.aspx" title="By section"  description="List of sections">
    <siteMapNode url="~/BrowsePublications.aspx?view=2" title="Publication view 1"  description="Publication view"/>
  </siteMapNode>
  <siteMapNode url="~/Officers.aspx" title="By responsible officer"  description="List of officers">
    <siteMapNode url="~/BrowsePublications.aspx?view=3" title="Publication view"  description="Publication view"/>
  </siteMapNode>
</siteMapNode>

and also using hashes, but since i am redirecting to BrowsePublications.aspx with query parametrs, ( my url is something like

BrowsePublications.aspx?view=2&c=24

asp.net did not recognize it and i got an exception when i tried accessing SiteMap.CurrentNode from BrowsePublications.aspx What is the easiest way to achieve this. thanks

1

There are 1 best solutions below

0
On

I struggled for a while to do what you have asked. There is an effective and easier way to have 2 same urls(with different query strings) in different sitemap nodes. Thought to post the solution so that anyone in future would benefit from it.

Firstly you need a parent node and a parent parent node already present in the sitemap.

<siteMapNode title="" description="" url="" >
        <siteMapNode title="ParentParentTest2" description="" url="~/ParentParentTest2.aspx" >       
           <siteMapNode title="ParentTest2" description="" url="~/ParentTest2.aspx" >
           <siteMapNode title="Page_that_needs_to_be_in_multiple_Nodes" description="" url="~/XYZ.aspx" />         
    </siteMapNode>
        </siteMapNode>

        <siteMapNode title="ParentParentTest2" description="" url="~/ParentParentTest2.aspx"  >
           <siteMapNode title="ParentTest2" description="" url="~/ParentTest2.aspx">
////you don't have to add the ~/XYZ.aspx  node again. If you add it then you will get a multiple node exception.
           </siteMapNode>
         </siteMapNode> 

    </siteMapNode>

//Copy this code in master page Page_Load event
SiteMap.SiteMapResolve += SiteMap_SiteMapResolve;

///Add this event handler to the master page.

  public SiteMapNode SiteMap_SiteMapResolve(object sender, SiteMapResolveEventArgs e)
            {

               //get current node
                SiteMapNode currentNode = SiteMap.CurrentNode;
                if (currentNode != null)
                {
                    currentNode = SiteMap.CurrentNode.Clone(true);
                    SiteMapNode tempNode = currentNode;


                        SiteMapNode nNewNode = new SiteMapNode(e.Provider,tempNode.Key);
                        switch (tempNode.Key)
                        {
                            case "/Test1.aspx":
                                    nNewNode.Clone(true);

                                    nNewNode.ParentNode.ParentNode.ReadOnly = false;
                                    nNewNode.ParentNode.ReadOnly = false;                           
    if(QueryString["Page"]="Test1")
    {    
                                    nNewNode.ParentNode.ParentNode = SiteMap.Provider.FindSiteMapNodeFromKey("~/ParentParentTest1.aspx");

                                    nNewNode.ParentNode = SiteMap.Provider.FindSiteMapNodeFromKey("~/ParentTest1.aspx");
                                    nNewNode.ParentNode.ReadOnly = false;
                                    nNewNode.ParentNode.Url = "~/ParentTest1.aspx?StateSave=true";
                                    nNewNode.ParentNode.Title = HttpContext.Current.Request.QueryString["ParentNodeName"];
                                    nNewNode.Title = HttpContext.Current.Request.QueryString["CurrentNodeName"];
    }

    if (QueryString["Page"]="Test2")
    {
    //Copy the same code and replace Test1 with Test2.
    }

    // returning the newly prepared Node.                         
                            return nNewNode;
                            default: break;

                        }

                    }

                }

                return currentNode;

            }