[.NET 2.0] Adding Web Parts programmatically (part 2)

I got a tip from Mike Harder on the ASP.NET Team to derive from CatalogPart and do my own catalog. So I tried that and it works pretty nice. I'd like to share my experience like this. Note that I'm a complete newbie when it comes to WebParts and the 2.0 documentation in this area is sort of under construction it seems :)

Anyway, first create a simple ASPX page with the basic stuff needed (I won't go into details of the manager and such):

<%@ Page Language="C#" CompileWith="MyCatalog.aspx.cs" ClassName="MyCatalog" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>Untitled Page</title>

</head>

<body>

    <form id="form1" runat="server">

        <asp:WebPartManager ID="WebPartManager1" Runat="server" OnDisplayModeChanged="WebPartManager1_DisplayModeChanged">

        </asp:WebPartManager>

        <asp:WebPartPageMenu ID="WebPartPageMenu1" Runat="server" Text="Display Mode" Mode="Menu">

            <HoverStyle BorderWidth="1" />

            <MenuStyle BorderWidth="1" />

            <BrowseModeVerb Text="Browse" />

            <DesignModeVerb Text="Design" />

            <EditModeVerb Text="Edit" />

            <CatalogModeVerb Text="Catalog" />

        </asp:WebPartPageMenu>

        <br />

        <h1>

            Web Parts Testing Page</h1>

        <br />

        <table cellspacing="0" cellpadding="0" border="0">

            <tr>

                <td valign="top">

                    <asp:WebPartZone ID="WebPartZone1" Runat="server" DragHighlightColor="244, 198, 96"

                        HeaderText="My Zone" HeaderStyle-Font-Bold="true">

                        <PartTitleStyle ForeColor="#666644"></PartTitleStyle>

                        <ZoneTemplate>

                        </ZoneTemplate>

                        <PartStyle BorderWidth="0px" BackColor="Yellow"></PartStyle>

                    </asp:WebPartZone>

                </td>

                <td valign="top">

                    <asp:EditorZone ID="EditorZone1" Runat="server">

                        <ZoneTemplate>

                            <asp:AppearanceEditorPart Runat="server" ID="AppearanceEditorPart1" />

                            <asp:LayoutEditorPart Runat="server" ID="LayoutEditorPart1" />

                        </ZoneTemplate>

                    </asp:EditorZone>

                    <asp:CatalogZone ID="CatalogZone1" Runat="server" HeaderText="Add Web Parts">

                    </asp:CatalogZone>

                </td>

            </tr>

        </table>

    </form>

</body>

</html>

 

It's very simple, and has only one WebPartZone in it. The code behind for the ASPX page looks like this:

using System;

using System.Web.UI.WebControls.WebParts;

 

public partial class MyCatalog

{

           void Page_Load(object sender, EventArgs e)

           {

                      //if we're in catalog mode, show our special catalog

                      if (WebPartManager1.DisplayMode.Name == "Catalog")

                      {

                                 AddMyCatalog();

                      }

           }

 

           /// <summary>

           /// Code to add our own catalog to the catalog zone

           /// </summary>

           private void AddMyCatalog()

           {

                      MyCatalogPart catalogPart = new MyCatalogPart();

                      catalogPart.ID = "mycatalog";

                      catalogPart.Title = "My Catalog";

 

                      CatalogZone1.CatalogParts.Add(catalogPart);

           }

 

           void WebPartManager1_DisplayModeChanged(object sender, WebPartDisplayModeEventArgs e)

           {

                      //if the user has changed display mode to catalog, add it to the catalog zone

                      if (WebPartManager1.DisplayMode.Name == "Catalog")

                      {

                                 AddMyCatalog();

                      }

 

           }

}

And finally, the implementation of MyCatalogPart. It is derived from CatalogPart and must implement GetAvailableWebPartDescriptions() and GetWebPart()

using System;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Collections;

 

/// <summary>

/// Test implementation of my own catalog part, which displays a dynamic list

/// of web part that I can get from a database, xml or by perhaps looking for

/// web parts assemblies on disk.

/// </summary>

public class MyCatalogPart : CatalogPart

{

 

           /// <summary>

           /// Returns a collection of available web parts in my catalog

           /// </summary>

           /// <returns></returns>

           public override WebPartDescriptionCollection GetAvailableWebPartDescriptions()

           {

                      //normally get a list of web parts dynamically from XML or database or something

                      WebPartDescription wpDescription = new WebPartDescription("MyWebPartID", "My Web Part", null);

                      ArrayList arrWpDescriptions = new ArrayList();

                      arrWpDescriptions.Add(wpDescription);

                      return new WebPartDescriptionCollection(arrWpDescriptions);

           }

 

           /// <summary>

           /// User has selected a web part in my catalog, return the correct instance of that web part

           /// </summary>

           /// <param name="description">The selected web part from the catalog</param>

           /// <returns></returns>

           public override WebPart GetWebPart(WebPartDescription description)

           {

                      if (description.ID == "MyWebPartID")

                      {

                                 //returning an instance of the selected web part

                                 return new MyWebPart();

                      }

 

                      return null;

           }

}

The very simple Web Part called MyWebPart is described in my previous blog post, but you can replace it with whatever web part you wish. Let me know if you try this one out and anything seems to crash. As I said, I'm new to this portal framework - haven't even looked at how things are done in SharePoint.

1 Comment

Comments have been disabled for this content.