SiteMapProvider.Initialize(String, NameValueCollection) Method

Definition

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

C#
public override void Initialize(string name, System.Collections.Specialized.NameValueCollection attributes);

Parameters

name
String

The Name of the provider to initialize.

attributes
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.

Examples

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.

C#
// 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;
}

Remarks

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(String, NameValueCollection) method in a derived class, be sure to first call the Initialize(String, NameValueCollection) method for the base class before performing your own initializations.

Applies to

Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

See also