views:

20

answers:

1

I have navigation defined in web.sitemap and I want programmaticly disable some siteMapNode. How is it possible?

For example I have next node: And I want disable node if it has roles="Admin"

roleManager is set up like <roleManager enabled="true" defaultProvider="AspNetWindowsTokenRoleProvider" /> and Windows Authorization does not have "Admin" group. It is like Virtual one.

I tried next:

    SiteMapNodeCollection tempCollection = new SiteMapNodeCollection(SiteMap.RootNode.ChildNodes);

    if (SiteMap.RootNode.ReadOnly)
        SiteMap.RootNode.ReadOnly = false;

    foreach (SiteMapNode node in tempCollection)
    {
        if (node.Roles.Contains("Admin"))
        {
            SiteMap.RootNode.ChildNodes.Remove(node);
        }
    }

Then I am getting: System.NotSupportedException: Collection is read-only.

Then tried like

    SiteMapNodeCollection modifiableCollection = new SiteMapNodeCollection(SiteMap.RootNode.ChildNodes);

    foreach (SiteMapNode node in SiteMap.RootNode.ChildNodes)
    {
        if (node.Roles.Contains("Admin"))
        {
            modifiableCollection.Remove(node);
        }
    }

But after that I dont know how to plug that new collection into navigation (Menu1 object)

Also tried to process every node throw handler, like

SiteMap.SiteMapResolve += new SiteMapResolveEventHandler(this.SiteMapAccess);

    private SiteMapNode SiteMapAccess(Object sender, SiteMapResolveEventArgs e)
    {
        SiteMapNode RootNode = SiteMap.RootNode.Clone(true);
        SiteMapNode tempNode = RootNode;

        if (tempNode.Roles.Contains("Admin"))
        {
            tempNode.RootNode.ChildNodes.Remove(tempNode);
        }
        return RootNode;
    }

But it is throwing that RootNode object should be created before using "new" statement. Although I used next guide: http://msdn.microsoft.com/en-us/library/ms178425.aspx

Any suggestions? http://forums.asp.net/t/894192.aspx here I found interesting discussion where I did not try option with e.Item.Parent.ChildItems.Remove(e.Item)

Here is also interesting example http://stackoverflow.com/questions/12297/how-can-i-remove-nodes-from-a-sitemapnodecollection, but my datasource have SiteMapDataSource type, not Repeater.DataSource

A: 

I realized it next:

I created new provider like

    public class MainSiteMap : StaticSiteMapProvider
    {
        SiteMapNode _root = null;

        public override SiteMapNode BuildSiteMap()
        {
            if (_root != null)
                return _root;

            Dictionary<Guid, SiteMapNode> nodes = new Dictionary<Guid, SiteMapNode>();
            Guid id = Guid.NewGuid();
            string[] roles = new string[] { "*" };

            _root = new SiteMapNode(this, id.ToString(), "~/Default.aspx", "Home", "RTD Home page");


            foreach (SiteMapNode node in SiteMap.Providers["RootSiteMap"].RootNode.ChildNodes)

            {
                if (!node.Roles.Contains("Admin"))
                {

                    AddNode(node, _root);
                }
            }

            _root.Roles = roles;
            nodes.Add(id, _root);
            AddNode(_root, null);

            return _root;
        }

        protected override SiteMapNode GetRootNodeCore()
        {
            BuildSiteMap();

            return _root;
        }
    }

and did Provider registration in web.config:

<siteMap defaultProvider="MainSiteMap" enabled="true">
  <providers>
    <add name="MainSiteMap" type="MainSiteMap" />
    <add name ="RootSiteMap" type="System.Web.XmlSiteMapProvider" siteMapFile="web.sitemap"/>
  </providers>
</siteMap>
Juri Bogdanov