[.NET 2.0] Creating your own CatalogPart
In two earlier posts (here and here), way back in beta1, I showed how to create your own CatalogPart which could display a list of available web parts from a database or something. Some people will be quite happy to add web parts in a declarative way, but I wanted a solution which were more dynamic. In the future I'll try to write some sample code where you create web parts in a separate library and just drop them in a directory somewhere in the portal, and the web parts gets picked up automatically in the CatalogZone without the need to change the code. Just as a proof of concept.
Anyway, I noticed that the code changed somewhat in beta2 compared to how it used to work back in beta1. There are two ways (at least) you can add your own CatalogPart to a web page:
- Create your own CatalogPart and declare it in the CatalogZone, in the aspx-page. This is the simplest way. See sample below.
- Create your own CatalogZone and declare it in the aspx-page. This CatalogZone then adds your CatalogZone declaratively or in run-time. A bit more coding and there is no need to do it this way unless you plan on doing something special within your CatalogZone. What that may be, I don't really know :)
So, sample code for MyCatalogPart:
using System;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections;
namespace MyCatalogZones
{
public class MyCatalogPart : CatalogPart
{
public override WebPartDescriptionCollection GetAvailableWebPartDescriptions()
{
//Generate a list of available web parts that shows
//up in the catalog, each with a unique ID
//This sample code just adds one web part
WebPartDescription wpDescription = new WebPartDescription("MyWebPartID", "My Web Part", "A description of the web part", null);
ArrayList arrWpDescriptions = new ArrayList();
arrWpDescriptions.Add(wpDescription);
return new WebPartDescriptionCollection(arrWpDescriptions);
}
public override WebPart GetWebPart(WebPartDescription description)
{
//a bit of "dummy" code to add a specific web part
//which maps against "MyWebPartID" from the catalog
if (description.ID == "MyWebPartID")
{
//returning an instance of the selected web part
MyWebParts.MyWebPart newPart = new MyWebParts.MyWebPart("My Web Part");
return newPart;
}
return null;
}
}
}
Note that in the code above, when you create the list, you may want to check if the user has already added an instance of the web part to his page before you add it to the list. This depends on if you want to let the users add a web part several times to the page or not. The web part may be such that it can be configured to show different content depending on its properties, so why not...
This is some sample code for adding your CatalogPart to a normal CatalogZone in an aspx-page:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register TagPrefix="jd" Namespace="MyCatalogZones" Assembly="MyCatalogZones" %>
<!-- lots of aspx code here... -->
<asp:CatalogZone ID="CatalogZone1" runat="server" >
<ZoneTemplate>
<asp:DeclarativeCatalogPart ID="DeclarativeCatalogPart1" runat="server">
<WebPartsTemplate>
<asp:label id="aPartInTheCatalog" runat="server" title="Catalog webpart">
<h2>Dummy part</h2>
<p>This is a part in the catalog.</p>
</asp:label>
</WebPartsTemplate>
</asp:DeclarativeCatalogPart>
<asp:PageCatalogPart ID="PageCatalogPart1" runat="server" />
<jd:MyCatalogPart ID="MyCatalogPart1" runat=server Description="My own dynamic catalog part" Title="My Special Parts Catalog" />
</ZoneTemplate>
</asp:CatalogZone>
<!-- lots of aspx code here... -->
You don't really have to have the DeclarativeCatalogPart and the PageCatalogPart there, but if you allow users to close your dynamically added parts, you may want to let them add them again, so the PageCatalogPart may be useful :)