There are a number of SharePoint classes that have a built-in caching functionality:
The PortalSiteMapProvider is a part of the SharePoint Publishing API and is typically used by navigation controls. All results returned by methods of the PortalSiteMapProvider are automatically stored in the object cache.
The SiteMapProviders for SharePoint are defined in the web.config and some of the attributes can be configured, for example the IncludePages attribute. By default it is set to false but setting it to true will allow the SiteMapProvider to return not only web nodes with but also the published pages in the Pages library.
In the OnInit method you can define which SiteMapProvider to use. But you can also use the current SiteMapProvider.
SiteMapProvider siteMapProvider = SiteMap.Providers[siteMapProviderName];
PortalSiteMapProvider portalProvider = PortalSiteMapProvider.CurrentNavSiteMapProvider;
You can also modify a number of properties of the SiteMapProvider within the OnInit method.
portalSiteMapProvider = siteMapProvider as PortalSiteMapProvider; portalSiteMapProvider.DynamicChildLimit = 0; portalSiteMapProvider.EncodeOutput = true; portalSiteMapProvider.IncludePages = PortalSiteMapProvider.IncludeOption.Always; portalSiteMapProvider.IncludeSubSites = PortalSiteMapProvider.IncludeOption.Never;
The construction of the navigation control is done in the CreateChildControls method. The following code snippet retrieves all pages with content type “Brewery Page” from the SiteMapProvider. By using the GetCachedListItemsByQuery method you specify that you want to use the built-in cache.
PortalWebSiteMapNode pagesNode = portalSiteMapProvider.CurrentNode as PortalWebSiteMapNode; // get all the brewery pages SPQuery query = new SPQuery(); query.Query = "<Where><Eq><FieldRef Name=\"ContentType\" />” + “<Value>Brewery Page</Value></Eq></Where>"; SiteMapNodeCollection pageNodes = portalSiteMapProvider.GetCachedListItemsByQuery( pagesNode, "Pages", query, SPContext.Current.Web);
All nodes returned by the provider are of type PortalSiteMapNode and may be safely cast to this type. The class exposes a number of properties, some of which are related to navigation specifically, but also properties like Title, Description and UniqueId. The Type property provides information about the node’s internal type, and may be used to determine if casting to a more specific type of PortalSiteMapNode is possible. Each specific type has its own additional properties.
In this code sample the GetCachedListItemsByQuery method specifically requires the pageNode to be of type PortalWebSiteMapNode, and that’s why it is casted to this specific type.