Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

SiteMapProvider.Initialize Method

Initializes the SiteMapProvider implementation, including any resources that are needed to load site map data from persistent storage.

Namespace:  System.Web
Assembly:  System.Web (in System.Web.dll)
public override void Initialize(
	string name,
	NameValueCollection attributes
)

Parameters

name
Type: System.String

The Name of the provider to initialize.

attributes
Type: System.Collections.Specialized.NameValueCollection

A NameValueCollection that can contain additional attributes to help initialize the provider. These attributes are read from the site map provider configuration in the Web.config file.

The Initialize method does not actually build a site map, it only prepares the state of the SiteMapProvider object to do so. The default implementation initializes the SecurityTrimmingEnabled property for the site map provider from the site navigation configuration.

Classes that derive from the SiteMapProvider can override the Initialize method to initialize any state and resources that are required to load site map data from persistent storage. For example, if your derived class is using files to store site map data, any file initialization can be performed in the Initialize method. If the derived class uses some other type of data store, such as a relational database, initializing a database connection might be performed.

Additional attributes, such as file names or connection strings, are read by the ASP.NET configuration system and passed to the Initialize method with its NameValueCollection parameter.

Notes to Inheritors

When overriding the Initialize method in a derived class, be sure to first call the Initialize method for the base class before performing your own initializations.

The following code example demonstrates how to override the Initialize method to prepare a Microsoft Access database connection.

The connection string for the OleDbConnection object is passed in the NameValueCollection parameter of the Initialize method. In this case, the connection string is provided by the provider-specific section in the Web.config file. Here, accessSiteMapConnectionString contains a connection string to a Microsoft Access database that hosts the site map data.

<siteMap defaultProvider="AccessSiteMapProvider">
  <providers>
     <add
       name="AccessSiteMapProvider"
       type="Samples.AspNet.AccessSiteMapProvider,Samples.AspNet"
       accessSiteMapConnectionString="PROVIDER=MICROSOFT.JET.OLEDB.4.0;DATA SOURCE=\\SomeUNCShare\\sitemap.mdb"/>
  </providers> 
 </siteMap>

This code example is part of a larger example provided for the SiteMapProvider class.

// Initialize is used to initialize the properties and any state that the 
// AccessProvider holds, but is not used to build the site map. 
// The site map is built when the BuildSiteMap method is called. 
public override void Initialize(string name, NameValueCollection attributes) {
    if (IsInitialized)
        return;

    base.Initialize(name, attributes);

    // Create and test the connection to the Microsoft Access database. 

    // Retrieve the Value of the Access connection string from the 
    // attributes NameValueCollection. 
    string connectionString = attributes[AccessConnectionStringName];

    if (null == connectionString || connectionString.Length == 0)
        throw new Exception ("The connection string was not found.");
    else
        accessConnection = new OleDbConnection(connectionString);

    initialized = true;
}

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft. All rights reserved.