Export (0) Print
Expand All

ImportCatalogPart Class

Imports a description file for a WebPart control (or other ASP.NET server control used as a WebPart control), so that users can add the control to a Web page with pre-defined settings. This class cannot be inherited.

Namespace: System.Web.UI.WebControls.WebParts
Assembly: System.Web (in system.web.dll)

public ref class ImportCatalogPart sealed : public CatalogPart
public final class ImportCatalogPart extends CatalogPart
public final class ImportCatalogPart extends CatalogPart
Not applicable.

The ImportCatalogPart control enables users to import a description file that describes settings on a WebPart control or server control that a user wants to add to a WebPartZoneBase zone.

After a user imports a description file, the WebPart control referenced in the file appears within the ImportCatalogPart control, and a user can add the control to the page.

The description file is not the same as the control itself. It is an XML file that ends with a .WebPart extension and contains name/value pairs--mostly property values--that describe the state of the control. The description file is created according to a specified XML format, as described in the topic Web Parts Control Description Files.

As for the control that the description file refers to, it can either be compiled into an assembly, or it can be a user control defined in an .ascx file. In either case, the control referenced in an imported description file must exist on the Web server that hosts the page that is attempting to import the control. The description file references the control name and the assembly (or file) that contains the control, and the description file contains settings that affect the control's property values, appearance, and behavior.

The ImportCatalogPart control enables users to share settings on controls. A complex control can have many properties and settings. For example, in a typical intranet site within a large company, a custom WebPart control might contain a number of properties that hold values specific to the users' environment, such as their database connections, departmental information, and so on. The control might also contain a number of properties that affect its appearance. One user could personalize the control on a particular site and get it working properly, export a description file for the control, and then share the description file with other users, who could import the file to add the fully configured control to other intranet sites that they are allowed to personalize. As long as the compiled assembly or user control file containing the control exists on the Web server that is hosting their site, users can add the control to other Web sites.

The mechanism by which users import a description file (and hence its associated server control) into a Web page is the ImportCatalogPart control, which a page developer must add to a Web page. When a user switches the page to catalog display mode, the ImportCatalogPart control appears, and the user can user this control to browse to the .WebPart description file corresponding to the server control they want to import. Following the UI and instructions provided by the ImportCatalogPart control, a user is able to add the desired server control into the Web page, with its appearance and properties fully configured as specified in the imported description file.

Before a WebPart control's description file can be imported, a user must first create (export) the file based on an existing WebPart control. A description file can be exported for a control if the following conditions are met:

  • The control has properties marked with the Personalizable attribute.

  • The Web.config file has the enableExport attribute value set to true on the <webParts> element.

  • A developer sets the value of the ExportMode property on the control to a value other than the default value of None, which prohibits export. If the ExportMode property value is set to NonSensitiveData, any property that contains an IsSensitive parameter with a Personalizable attribute is not exported when a user exports a description file. This enables control developers to prevent sensitive data, such as connection strings, from being exported in certain situations.

A user can export a control that has been enabled for export by clicking the export verb that appears in the verbs menu of the control, and following the instructions to save a .WebPart description file for the control. Other users can then import this file to configure their own instances of the control.

The ImportCatalogPart class contains several properties. The BrowseHelpText property contains text with instructions for users when they browse to locate the description file. The ImportedPartLabelText property contains text that serves as a label for the imported control as it appears within the ImportCatalogPart control. The PartImportErrorLabelText contains text that is displayed if an error occurs when a control description is being imported. The Title property overrides the base property to assign a default title for an ImportCatalogPart control if the developer does not assign a title. The UploadButtonText property contains the text for the button that the user clicks to upload the description file, and the UploadHelpText property contains the instructions for the upload process.

The ImportCatalogPart class also contains several unique methods. The GetAvailableWebPartDescriptions method retrieves a WebPartDescription object for each WebPart control in the catalog, which enables an ImportCatalogPart control to display information about each server control without having to create an instance of it. The GetWebPart method gets an instance of a particular WebPart control, based on the description passed to the method.

There are some inherent risks associated with using the ImportCatalogPart control. One example is the possibility of importing malicious data into your Web application through the description files used for importing. If someone has placed malicious script code as the value of a string property in the description file, then that script could potentially be executed when a user imports the description file and adds the referenced server control to a Web page. To minimize the risk of importing description files with malicious data, server controls that have string-typed properties should always encode the property data. Another risk involves importing types through description files (see Web Parts Control Description Files). A malicious user could submit requests to load many assemblies into the AppDomain, resulting in an excessive amount of memory being consumed.

To avoid the risks associated with import, you can disable the feature altogether by not using the import feature or the ImportCatalogPart control. Or you can limit what users have access to the control. You could do this programmatically, using role management (see Managing Authorization Using Roles). For instance, when the page loads, you could test to see whether a user is in a certain role, such as the administrator role. If the user is in the role, you could programmatically add the ImportCatalogPart control to the page for that user. You could also use a declarative approach to limit the set of users that can use the ImportCatalogPart control. Within your web page that contains a catalog, you could place two CatalogZone controls: one for users who can import, and one for those who cannot. The zone for users who can import would contain the ImportCatalogPart control. The zone itself could be placed inside of a LoginView control, which would enable you to limit use of the control in the zone to only those authenticated users or roles that you specify.

TopicLocation
How to: Set the Display Mode of a Web Parts PageBuilding ASP .NET Web Applications
How to: Export Web Parts Control SettingsBuilding ASP .NET Web Applications
How to: Set the Display Mode of a Web Parts PageBuilding ASP .NET Web Applications
How to: Export Web Parts Control SettingsBuilding ASP .NET Web Applications

The following code example demonstrates how to use the ImportCatalogPart control declaratively and programmatically on a Web page. The example has four parts:

  • A user control that enables you to change display modes on a Web Parts page.

  • A Web page that contains a CatalogZone control and an ImportCatalogPart control.

  • A source code file that contains two custom WebPart controls.

  • An explanation of how the example works when you load the page in a browser.

The first part of this code example is the user control that enables users to change display modes on a Web page. You should place the following source code in a file and name it Displaymodemenucs.ascx or Displaymodemenuvb.ascx (depending on which language you are using). For details about display modes and a description of the source code in this control, see Walkthrough: Changing Display Modes on a Web Parts Page.

No code example is currently available or this language may not be supported.

The second part of the code example is the Web page. At the top of the page are two register directives, one for the user control and one for the compiled component that contains the two custom WebPart controls. Notice that the page has a declarative reference to the ImportCatalogPart control, nested within the proper hierarchy of declarative elements. Notice also that several property values are assigned declaratively on the <asp:importcatalogpart> element. Also, the Button1_Click method updates a number of property values on the ImportCatalogPart control.

In the page's WebPartZone control, the two custom WebPart controls are declared. The <aspSample:userinfowebpart> control has an exportmode="all" attribute on it. This attribute is required to enable users to export a description file for the control, which can then be imported by other users who wish to import the control using the description file.

NoteNote:

To enable users of a Web Parts application to export a description file for WebPart controls, you must also enable the export feature in the Web application by adding an enableExport="true" attribute to the <webParts> element (which is a child of the <system.web> element) in the Web.config file. Export is disabled by default, so if you have not yet enabled export for your application, edit the Web.config file and do it now.

No code example is currently available or this language may not be supported.

The third part of the code example is the source code for the two WebPart controls. Notice that some properties on these controls are marked with the WebBrowsable attribute. This enables the PropertyGridEditorPart control to dynamically generate the user interface (UI) for a user to edit those properties when the controls are in edit mode. The properties are also marked with a WebDisplayName attribute, to specify the text of the label that appears next to each control in the editing UI. For the code example to run, you must compile this source code. You can compile it explicitly and put the resulting assembly in your Web site's Bin folder or the global assembly cache. Alternatively, you can put the source code in your site's App_Code folder, where it will be dynamically compiled at run time. This code example uses dynamic compilation. For a walkthrough that demonstrates both methods of compiling, see Walkthrough: Developing and Using a Custom Server Control.

The custom control called TextDisplayWebPart is referenced on the Web page with an <aspSample:TextDisplayWebPart> element. The other control, called UserInfoWebPart, is also declared on the Web page initially, though you will remove it later to demonstrate the ability to import a description file for a control.

No code example is currently available or this language may not be supported.

Now run the code example. Load the Web page in a browser. The first step is to edit the UserInfoWebPart control. Use the Display Mode drop-down list control and select Edit to switch the page to edit mode. Click the verbs menu of the UserInfoWebPart control (the downward arrow in the title bar), and then click Edit. When the editing UI appears, several editing controls appear below the UserInfoWebPart control that you can use to edit its field values. Edit some fields, click OK, and then use the Display Mode drop-down to return the page to browse mode.

The second step is to export a .WebPart description file for the UserInfoWebPart control. Click the verbs menu on the custom control (represented by the downward arrow in the title bar), and click Export. Follow the instructions to save a .WebPart description file for the control. Now close the Web page, and edit the page source in an editor. Delete the <aspSample:userinfowebpart> control declaration element, then save and close the file. (You are doing this step to simulate a user who does not yet have the UserInfoWebPart control, so you can import the control to the page).

Load the Web page again in a browser. The UserInfoWebPart control should not appear, because you removed it. Use the Display Mode drop-down list control and select Catalog to switch the page to catalog mode. In the ImportCatalogPart control, click the Browse button, and browse to the .WebPart file you created, then click the Upload button. A reference to the control should appear with a check box next to it. Select the check box, then click Add to add the control to the page.

While you are in this view of the page, click the Update ImportCatalogPart button near the bottom of the page to see the effect of programmatically updating a number of property values on the ImportCatalogPart control. After clicking the button, observe how the various properties are changed in the UI.

Finally, click Close to exit catalog mode and return the page to browse mode. The UserInfoWebPart control should now appear in the page, containing the values that it had when you exported it earlier.

System.Object
   System.Web.UI.Control
     System.Web.UI.WebControls.WebControl
       System.Web.UI.WebControls.Panel
         System.Web.UI.WebControls.WebParts.Part
           System.Web.UI.WebControls.WebParts.CatalogPart
            System.Web.UI.WebControls.WebParts.ImportCatalogPart

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

Community Additions

ADD
Show:
© 2014 Microsoft