Microsoft KB Archive/910446

= Loading Web Parts into a catalog at run time =

Article ID: 910446

Article Last Modified on 3/16/2007

-

APPLIES TO


 * Microsoft ASP.NET 2.0

-



Loading Web Parts into a catalog at run time
''To customize this column to your needs, we want to invite you to submit your ideas about topics that interest you and issues that you want to see addressed in future Knowledge Base articles and Support Voice columns. You can submit your ideas and feedback using the Ask For It form. There's also a link to the form at the bottom of this column.''



INTRODUCTION
Welcome to the ASP.NET Support Voice column! I am Jerry Orman. I have been with Microsoft for over five years and have spent most of my time focused on Web-related technologies such as Microsoft FrontPage and Microsoft SharePoint Products and Technologies. I've spent the past year working with Microsoft ASP.NET as a support engineer.

ASP.NET 2.0 introduces the Web Part framework outside of the SharePoint environment. This allows developers to build applications that are configurable by the user at run time. The CatalogPart class is used to provide a list of Web Parts to users at run time. Out of the box, ASP.NET 2.0 does not provide a catalog that is populated at run time. This Support Voice column will discuss how to create a custom catalog for this purpose.



MORE INFORMATION
Out of the box, ASP.NET 2.0 provides the following catalogs:  DeclarativeCatalogPart

This class allows you to declaratively add the Web Parts to the .aspx page at design time. You can also set the WebPartsListUserControlPath property and point to a user control (.ascx) that has Web Parts declaratively added at design time. For more information, visit the following Microsoft Developer Network (MSDN) Web site:

http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.webparts.declarativecatalogpart.aspx

 PageCatalogPart

This class displays Web Parts that were closed by a user. For more information, visit the following MSDN Web site:

http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.webparts.pagecatalogpart.aspx

 ImportCatalogPart

This class allows you to import a Web Part that you have exported. Exporting a Web Part allows you to preserve the settings, which is useful for highly customized Web Parts. For more information, visit the following MSDN Web site:

http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.webparts.importcatalogpart.aspx



So, as you can see, there is nothing out of the box that will allow you to drop DLLs into a folder and have them get picked up as Web Parts at run time. In order to do this, you need to create a custom CatalogPart class and override the following methods:  GetAvailableWebPartDescriptions

This method returns the Web Part descriptions used to populate the catalog in the CatalogZone. In this sample, we'll use reflection in this method to load the DLLs and determine if they contain Web Parts. For more information, visit the following MSDN Web site:

http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.webparts.catalogpart.getavailablewebpartdescriptions.aspx

 GetWebPart

This method returns the actual Web Parts to be rendered in the page. If you don't handle populating the collection that returns the Web Part correctly, your catalog will render and display the descriptions, but adding a Web Part to a WebPartZone will not do anything. For more information, visit the following MSDN Web site:

http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.webparts.catalogpart.getwebpart.aspx

</ul>

The following sample demonstrates how to build a custom catalog that uses reflection to populate the list of Web Parts at run time. This sample is provided as-is and is meant for demonstration purposes only. The following file is available for download from the Microsoft Download Center:

Download the CustomCatalogSample.exe package now.

For more information about how to download Microsoft support files, click the following article number to view the article in the Microsoft Knowledge Base:

119591 How to obtain Microsoft support files from online services

Microsoft scanned this file for viruses. Microsoft used the most current virus-detection software that was available on the date that the file was posted. The file is stored on security-enhanced servers that help prevent any unauthorized changes to the file.

We'll walk through the sample projects so you can get an idea of what you're getting into. The code is fully commented, which should make for some great reading.

The sample includes three projects:  MyControls class project  ReflectionCatalogPart

This is the class that does the work for the catalog.</li></ul> </li> SampleWebParts class project  RSSPart.cs

This is a Web Part I wrote that will go out and pull data from an RSS2 feed. You could flesh this out more to add different templates and caching.</li> FeaturedProductPart.cs, ICategoryInterface.cs, and SelectCategory.cs

These are from the following MSDN article:

http://msdn2.microsoft.com/en-us/library/ms379628(VS.80).aspx

I included them here to flesh out the list of Web Parts.</li></ul> </li> CustomParts Web site  Default.aspx

This file drives the sample. It has all the various catalogs and parts already declared on the page.</li> MyControls  CatalogTemplate.ascx

This file is used in the WebPartsListUserControlPath property of the DeclarativeCatalogPart class to demonstrate the use of that property.</li> ReportGenerator.ascx

This file has a GridView control to show using an .ascx file as a Web Part. (Any control that is not a Web Part is wrapped in the GenericWebPart class when it is placed in a WebPartZone.)</li></ul> </li></ul> </li></ul>

To use the sample, follow these steps:
 * 1) Open the CustomParts Web site as a file-based project in Microsoft Visual Studio 2005.
 * 2) Add the MyControls class project and the SampleWebParts class project as projects to the solution. To do this, click File, click Add, and then click Existing Project.
 * 3) Expand CustomParts Web Site.
 * 4) Double-click the Web.config file.
 * 5) Change the connection string to point to the instance of Microsoft SQL Server hosting the Northwind database.
 * 6) Browse the Default.aspx file.
 * 7) Click Login.
 * 8) Log in by typing BobSmith and the password BobSmith!.
 * 9) On the Modify Web Parts menu, click Catalog.

You'll have two catalogs, the Reflection Parts catalog and the Declarative Parts catalog.

You can view the ReflectionCatalogPart.cs file in the MyControls class and the asp:CatalogZone section in the Default.aspx file to see how the custom catalog works. In general, it checks to determine if there is an HttpContext and then uses reflection to populate and cache the Web Parts that are available in the /bin folder. You can add Web Parts by adding the DLL to the /bin folder. This will trigger an AppDomain recycle, which will clear the cache and allow the catalog to repopulate it on the next load.

Now that you have a sample demonstrating how to populate the catalog using reflection, you can leverage this to build catalogs that may better suit your needs.

<div class="moreinformation_section">

As always, feel free to submit ideas on topics you want addressed in future columns or in the Knowledge Base using the Ask For It form.

Keywords: kbhowto kbasp KB910446

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.