Tip on adding role support to your custom DynamicNodeProvider

Nov 6, 2011 at 11:34 AM

Great work on the SiteMap Provider! Not sure how actively the code are being worked on but here's a tip for an improvement. I recently needed to restrict access based on roles on my DynamicNodeProvider. The problem is that when building the DynamicNodeCollection I have no access to setting roles.

This is what I did to fix it:

On DynamicNode i added a property:

	public string[] Roles { get; set; }
And in DefaultSiteMapProvider.cs I added (gray is existing code):
public class DefaultSiteMapProvider : StaticSiteMapProvider

	/// <summary>
	/// Adds the dynamic nodes for node.
	/// </summary>
	/// <param name="node">The node.</param>
	/// <param name="parentNode">The parent node.</param>
	private IEnumerable<SiteMapNode> AddDynamicNodesFor(SiteMapNode node, SiteMapNode parentNode)

		// Build dynamic nodes
		var mvcNode = node as MvcSiteMapNode;
		foreach (var dynamicNode in mvcNode.DynamicNodeProvider.GetDynamicNodeCollection().ToList())

			// Added to support dynamic roles:
			if (dynamicNode.Roles != null && dynamicNode.Roles.Length > 0)
				clone.Roles = dynamicNode.Roles;


		// Done!
		return createdDynamicNodes;

In my DynamicNodeProvider I can then:

	var node = new DynamicNode
			Key = "<key>",
			Title = "<title>",
			Roles = new[] { "Role1", "Role2", "..." }


Works great for me and might be useful for others as well. Hopefully you can add this functionality or include me on the list of devs so I can update the code.

Nov 22, 2011 at 7:27 AM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.