Creating the Web Part
We now have the Web Part project set up with the necessary references, so let’s create the Web Part.
1. | In Solution Explorer, right-click on the project name, and select Add New Item.
|
2. | Select the Web Part file template from the Code category, name it NavigationWebPart.cs, and click Open.
|
If you choose Consumer/Provider Web Part or Tool Part, several interfaces will be implemented to enable Web Part communication. For the navigation Web Part, we can use the generic Web Part template as it does not need to communicate with other Web Parts.
You should now see the NavigationWebPart.cs
file in Solution Explorer and the code should be open in code view. As
you will see from the generated code, a Web Part inherits from Microsoft.SharePoint.WebPartPages.WebPart, which in turn inherits from System.Web.UI.Control.
The NavigationWebPart class has attributes that are used by the Web Part infrastructure and Visual Studio .NET. Please remove the attribute DefaultProperty("Text") as the navigation Web Part will not have a default property. The class attributes should now be:
[ToolboxData("<{0}:NavigationWebPart runat=server></{0}:NavigationWebPart>"),
XmlRoot(Namespace="ExtensibleMCMSPageListingWebPart")]
The ToolboxData
attribute defines the display name of the Web Part for use in the
Visual Studio .NET Toolbox. As the properties on the class will be
serialized into the Web Part Storage System, we must define the XML
namespace using the XmlRoot attribute.
Defining Custom Properties for the Web Part
We will now define and
develop the custom properties that will be used to configure this Web
Part. The table below lists all the custom properties that our Web Part
will expose.
Property | Description | Data type | Default Value | Category |
---|
Mode | The Web Part mode that is used to specify data sources and exception handling, primarily used for troubleshooting | enmMode {DebugWithTestData, DebugShowXml, DebugShowException, Production} | Production | MCMS Listing Mode |
SortBy | The sort order of the ChannelItemCollection | enmSortBy {Default, ChangeDate, DisplayName, DisplayPath, ExpiryDate, Importance, Ordinal, StartDate} | Default | MCMS Listing Configuration |
Startchannel | The channel to list the child items of | String | /Channels/ | MCMS Listing Configuration |
PresentationXsl | The XSL stylesheet used to control the presentation of the data | String | Load from DefaultXsl.xslt | MCMS Listing Presentation |
Testxml | XML for testing the transformation without connecting to MCMS | String | Load from TestXml.xml | MCMS Listing Presentation |
After the Web Part has been added to a Web Part page, its custom properties can be modified in the browser by selecting Modify Shared Web Part (or Modify Personal Web Part) from the top right corner of the Web Part.
As
you can see above, the controls that are used for each property will
vary based on the data type. The property we specified earlier, Mode, is of type enmMode,
which has four possible values so the ToolPane renders a drop-down
list. The ToolPane will also render a textbox for numbers and strings.
Custom properties can be grouped using the Category attribute on the property and this allows us to group multiple properties, for example MCMS Listing Configuration.