Using Site Maps

Using the SiteMapDataSource Control
The SiteMapDataSource control enables you to represent a Site Map declaratively in a page. You can bind navigation controls such as the TreeView and Menu controls to a SiteMapDataSource control. You also can bind other controls such as the GridView or DropDownList control to a SiteMapDataSource control.

Setting SiteMapDataSource Properties
The SiteMapDataSource control includes several valuable properties that you can set to modify the nodes that the control returns:
  • ShowStartingNode—Enables you to hide the starting node.
  • StartFromCurrentNode—Enables you to return all nodes starting from the current node.
  • StartingNodeOffset—Enables you to specify a positive or negative offset from the current node.
  • StartingNodeUrl—Enables you to return all nodes, starting at a node associated with a specified URL.

The most useful of these properties is the ShowStartingNode property. Normally, when you display a list of nodes with a Menu or TreeView control, you do not want to display the starting node (the link to the home page).


Using the SiteMap Class
Under the covers, the SiteMapDataSource control represents the contents of the SiteMap class. The SiteMap class represents an application’s Site Map regardless of whether the Site Map is stored in an XML file, a database, or some other data source. The class is a memory-resident representation of Site Map data. All the properties exposed by the SiteMap class are shared (static) properties:
  • CurrentNode—Enables you to retrieve the SiteMapNode that corresponds to the current page.
  • Enabled—Enables you to determine whether the Site Map is enabled.
  • Provider—Enables you to retrieve the default SiteMapProvider.
  • Providers—Enables you to retrieve all the configured SiteMapProviders.
  • RootNode—Enables you to retrieve the root SiteMapNode.

The CurrentNode and RootNode properties return a SiteMapNode object. Because a Site Map can contain only one root node, and the root node contains all the other nodes as children, the RootNode property enables you to iterate through all the nodes in a Site Map. The Provider property returns the default SiteMapProvider. You can use this property to access all the properties and methods of the SiteMapProvider class, such as the FindSiteMapNode() and GetParentNode() methods. The SiteMap class also supports a single event:
  • SiteMapResolve—Raised when the current node is accessed.

Using the SiteMapNode Class
All pages and folders in a Site Map are represented by instances of the SiteMapNode class. The SiteMapNode class contains the following properties:
  • ChildNodes—Returns the child nodes of the current node.
  • Description—Returns the description of the current node.
  • HasChildNodes—Returns True when the current node has child nodes.
  • Item—Returns a custom attribute (or resource string).
  • Key—Returns a unique identifier for the current node.
  • NextSibling—Returns the next sibling of the current node.
  • ParentNode—Returns the parent node of the current node.
  • PreviousSibling—Returns the previous sibling of the current node.
  • Provider—Returns the SiteMapProvider associated with the current node.
  • ReadOnly—Returns true when a node is read-only.
  • ResourceKey—Returns the resource key associated with the current node (enables localization).
  • Roles—Returns the user roles associated with the current node.
  • RootNode—Returns the Site Map root node.
  • Title—Returns the title associated with the current node.
  • Url—Returns the URL associated with the current node.

The SiteMapNode class also supports the following methods:
  • Clone()—Returns a clone of the current node.
  • GetAllNodes()—Returns all descendent nodes of the current node.
  • GetDataSourceView()—Returns a SiteMapDataSourceView object.
  • GetHierarchicalDataSourceView()—Returns a SiteMapHierarchicalDataSourceView.
  • IsAccessibleToUser()—Returns True when the current user has permissions to view the current node.
  • IsDescendantOf()—Returns True when the current node is a descendant of a particular node.

Advanced Site Map Configuration
This section explores several advanced features of Site Maps. For example, you learn how to display different SiteMap nodes, depending on the roles associated with the current user. You also learn how to create multiple Site Maps for a single application. Finally, you learn how you can extend Site Maps with custom attributes.

Using Security Trimming
You might want to display different navigation links to different users, depending on their roles. For example, if a user is a member of the Administrators role, you might want to display links to pages for administrating the website. However, you might want to hide these links from other users. To display different links to different users depending on their roles, you must enable a feature of Site Maps named Security Trimming.



Merging Multiple Site Maps
To make it easier to manage a large application, you can store Site Maps in more than one location and merge the Site Maps at runtime. For example, if you are using the default SiteMapProvider—the XmlSiteMapProvider—then you can create multiple sitemap files that describe the navigation structure of different sections of your website.

You also can associate different SiteMapProviders with different nodes in a sitemap file by taking advantage of the provider attribute. For example, a Site Map might be stored in a database table for one section of your website and stored in an XML file for another section of your website.

Creating Custom Site Map Attributes
You can extend a Site Map with your own custom attributes. You can use a custom attribute to represent any type of information that you want. For example, imagine that you want to associate <meta> Description tags with each page in your web application to make it easier for search engines to index your website. In that case, you can add a metaDescription attribute to the nodes in a Web.sitemap file.

Creating Custom Site Map Providers
Site Maps use the provider model. This means that you can easily modify or extend the way Site Maps work by creating your own Site Map provider. In this section, we create two custom Site Map providers. First, we create the AutoSiteMapProvider. This provider automatically builds a Site Map based on the file and folder structure of a website. Next, we create a SqlSiteMapProvider. This provider enables you to store a Site Map in a Microsoft SQL Server database table instead of an XML file.

Creating the AutoSiteMapProvider
All Site Map providers inherit from the base SiteMapProvider class. If you want to create your own Site Map provider, then you can override the methods of this base class. FIGURE 20.7 Extending a Site Map with a <meta> tag.

However, in most cases it makes more sense to derive a custom Site Map provider from the base StaticSiteMapProvider class. This is the base class for the default Site Map provider—the XmlSiteMapProvider—and this class includes default implementations of many of the SiteMapProvider methods. This AutoSiteMapProvider derives from the StaticSiteMapProvider class. It overrides two methods of the base class: GetRootNodeCore() and BuildSiteMap(). The GetRootNodeCore() method returns the root node of the Site Map. The BuildSiteMap() method is the method that is actually responsible for building the Site Map.

Creating the SqlSiteMapProvider
For certain applications it makes more sense to store a Site Map in a database table than an XML file. In this section, you can see the creation of the SqlSiteMapProvider, which stores a Site Map in a Microsoft SQL Server database.

Generating a Google SiteMap File
Google provides a free service, named Google SiteMaps, that you can use to monitor and improve the way that Google indexes the pages on your website. For example, you can use Google SiteMaps to discover which Google search queries have returned pages from your website and the ranking of your pages in Google search results. You also can use Google SiteMaps to view any problems that the Google crawler encounters when indexing your site.

You can sign up for Google SiteMaps by visiting the following URL:

To use Google SiteMaps, you must provide Google with the URL of a Google SiteMap file hosted on your website. The Google SiteMap file is an XML file that contains a list of URLs you want Google to index.

The Google SiteMap XML file has the following format:

<urlset xmlns=”http://www.google.com/schemas/sitemap/0.84”>
<url>
<loc>http://www.example.com/</loc>
<lastmod>2005-01-01</lastmod>
</url>
<url>
<loc>http://www.example.com/sample.html/</loc>
<lastmod>2006-03-11</lastmod>
</url>
</urlset>

The Google SiteMap file contains a simple list of <url> elements that contain <loc> elements representing the location of the URL and <lastmod> elements representing the last modified date of the URL.

The Google SiteMap file also can contain <changefreq> and <priority> elements. The <changefreq> element indicates how frequently a URL changes, and the <priority> element represents the priority of a URL relative to other URLs in your site. These elements are optional and are ignored here.

You can generate a Google SiteMap file automatically from an ASP.NET SiteMap. The HTTP Handler in the following Listing generates a Google SiteMap that conforms to Google’s requirements for a valid SiteMap file.



The HTTP Handler in Listing generates an XML file by iterating through each of the nodes in an ASP.NET Site Map. The XML file is created with the help of the XmlWriter class. This class is used to generate each of the XML tags.

You can think of an HTTP Handler is a lightweight ASP.NET page.

The file in Listing contains the XML file returned by the PublicSiteMap.ashx handler when the Handler is called from the sample application.
When you sign up at the Google SiteMaps website, submit the URL of the PublicSiteMap.ashx file when you are asked to enter your SiteMap URL. The Google service retrieves your SiteMap from the handler automatically.





No comments:

Post a Comment