Creating the SiteMap according to user role
SiteMaps are basically xml files with the extension .sitemap which dictates the navigation of the application. Simply, add a new sitemap file which by default will be named Web.sitemap. Please note that the Web.sitemap file must be placed in the root directory of the application as you can also see in the above screen shot. Let's look at what the Web.sitemap file contains:
Each sitemap file must contain a root siteMapNode element which I have made bold in the code above. Since, this article demonstrates that how you can create menus based on the role of the user that is why I have created two more sitemap files namely Admin.sitemap and User.sitemap which are placed in the Navigation folder. The main sitemap file "Web.sitemap" contains a reference to the other two files. This reference is created by using the siteMapFile attribute. I have used "~/" which means that ASP.NET will search for the Navigation folder starting from the root of the application.http://www.casa.uh.edu/ApCalculus2/Teachers/ViewStudentGrades1.aspx
Now, let's see the what does Admin.sitemap and User.sitemap contains:
The above Admin.sitemap file contains several nodes which makes up the navigation for the pages. The root node url must be unique in all the sitemap files. The above Admin.sitemap file will create a menu shown by the skeleton below:
Home Admin Functions
Add User
Delete User
Update User
The User.sitemap is also similar to the Admin.sitemap file but creates different links with different captions.
Okay, so we have set up our Web.sitemap, Admin.sitemap and User.sitemap and now we are ready to populate a data source with the SiteMap.
Populating Sitemap Data Source:
We will populate the data source based on the role of the user. This means that if Admin logs in then Admin.sitemap file will be used to create the navigation for him. On the other hand if User logs in then User.sitemap file will be used to create the navigation for him. I have created a simple class "NavigationManager" which deals with creating the navigation for different kinds of users. NavigationManager class exposes a method "GetSiteMapDataSource" which returns the data source to the navigation control which in this case will be a Menu control. Let's see how "GetSiteMapDataSource" method is implemented:
public static SiteMapDataSource GetSiteMapDataSource(string role)
{
string url = String.Empty;
if (role.Equals("Admin"))
url = "~/Admin/AdminHomePage.aspx";
else if (role.Equals("User"))
url = "~/User/UserHomePage.aspx";
XmlSiteMapProvider xmlSiteMap = new XmlSiteMapProvider();
System.Collections.Specialized.NameValueCollection myCollection = new System.Collections.Specialized.NameValueCollection(1);
myCollection.Add("siteMapFile", "Web.sitemap");
xmlSiteMap.Initialize("provider", myCollection);
xmlSiteMap.BuildSiteMap();
SiteMapDataSource siteMap = new SiteMapDataSource();
siteMap.StartingNodeUrl = url;
/* This will not show the starting node and hence giving it
* the horizontal cool look :)
* */
siteMap.ShowStartingNode = false;
return siteMap;
}
The GetSiteMapDataSource method takes "role" as an argument. If the role is "Admin" then we assign it a certain url. The assigned url is the same as we have put for the root node in the Admin.sitemap. If the role is "User" then we assign it a url which is the root node url in the User.sitemap file.
Another important point to note in the above code is that I have assigned the url to the siteMap.StartingNodeUrl property. This means that it will start building the navigation from the url which is gets based on the role of the user. Also the siteMap.ShowStartingNode is set to false. This is because we don't want to show the root node to the user. If you show the root node then you will not be able to set the Menu control to display horizontally properly.
