asp tutorials, asp.net tutorials, sample code, and Microsoft news from 15Seconds
Data Access  |   Troubleshooting  |   Security  |   Performance  |   ADSI  |   Upload  |   Email  |   Control Building  |   Component Building  |   Forms  |   XML  |   Web Services  |   ASP.NET  |   .NET Features  |   .NET 2.0  |   App Development  |   App Architecture  |   IIS  |   Wireless
 
Pioneering Active Server
 Power Search








Active News
15 Seconds Weekly Newsletter
• Complete Coverage
• Site Updates
• Upcoming Features

More Free Newsletters
Reference
News
Articles
Archive
Writers
Code Samples
Components
Tools
FAQ
Feedback
Books
Links
DL Archives
Community
Messageboard
List Servers
Mailing List
WebHosts
Consultants
Tech Jobs
15 Seconds
Home
Site Map
Press
Legal
Privacy Policy
internet.commerce














internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

HardwareCentral
Compare products, prices, and stores at Hardware Central!

Customizing SharePoint Web Parts with Custom Properties
By Gayan Peiris
Rating: 14. out of 5
Rate this article


  • email this article to a colleague
  • suggest an article

    Introduction

    Developers have the flexibility to create custom Web Parts to achieve sophisticated custom functionality in SharePoint Portal server. Typically this will be used to create common customized functionality for a specific organization or project. There are several properties available by default from SharePoint Portal server to customize the Web Part. Alternatively, developers can create their own custom properties to achieve extra customization on Web Part appearance and behavior.

  • download source code

    Properties

    The Web Part base class provides a set of default properties which allow users to change appearance and behavior. These properties are found in the default Web Part property pages and contain basic customization properties, such as height, width, frame set state and much more (as illustrated below).

    Figure 1: Properties available by default in the "default" property pane.

    TIP: To access the property pane, select the Edit mode of the Web Part. Then click the down arrow located at top right hand corner of the Web Part. Select "Modify Shared web part" (or personnel depend on witch mode the Web Part is running).

    The properties are categorized according to their behavior and functionality, which provides a logical structure when you need to find the right item. According to Figure 1, all the properties that affect appearance are listed below the "Appearance" category and all the advanced options under the "Advanced" category.

    While the default properties are a good starting point, it won't take long until you come across development requirements which the default properties cannot satisfy. This is where you implement Custom Properties to achieve the additional functionality or behavior required.

    Custom Properties

    A Custom Property is a property that a developer creates when they need to introduce additional functionality or behavior to the Web Part that is not provided by the base class.

    The Visual Studio .Net environment supports the development of Web Parts for SharePoint Portal Server. The Web Part Template provided on MSDN (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odc_sp2003_ta/html/sharepoint_webparttemplates.asp) provides an excellent starting point with the structure of a custom Web Part property (example below).

                ///<summary>

                /// Default property available form the template

                ///</summary>

                [Browsable(true),Category("Miscellaneous"),

                DefaultValue(defaultText),

                WebPartStorage(Storage.Personal),

                FriendlyName("Text"),Description("Text Property")]

                publicstring Text

                {

                      get

                      {

                            return text;

                      }

     

                      set

                      {

                            text = value;

                      }

                }

    Figure 2: Default custom property available from MSDN template.

    Before we start developing any custom properties, let's have a look at how custom property works. The custom properties can be customized and saved in SharePoint as XML. The System.Xml.Serialization namespace is used to serialize the Custom Properties to XML. The above namespace will be available from System.Xml.dll assembly. There is a XML namespace attribute defined at the root level of the assembly as displayed below,

          ///<summary>

          /// Customize a web part

          ///</summary>

          [DefaultProperty("Text"),

     ToolboxData("<{0}:CustomizedWebPart  runat=server></{0}:CustomizedWebPart>"),XmlRoot(Namespace="CustomizedWebPart")]

          publicclass Customer : Microsoft.SharePoint.WebPartPages.WebPart

          {

    Figure 3: XML namespace in root level

    The namespace attribute can be defined, either at the root level or at the property level. If a root level XML namespace is available, the developer doesn't need to assign XML namespaces at property level. But if the developer assigns a XML namespace at the property level, this will override the XML namespace assigned at the root level.

    If you are using the template provided by MSDN, the System.Xml.dll will be automatically added to your reference in Web Part project as display below,

    Figure 4: System.Xml.dll in Web Part project reference list

    There are two parts to create a Custom Property:

    1. Create property accessors

    Each Custom Property of the Web Part requires the appropriate get and set accessors methods.

                publicstring Text

                {

                      get

                      {

                            return text;

                      }

     

                      set

                      {

                            text = value;

                      }

                }

    Figure 5: Get and Set accessors methods for a Web Part property in C#

    2. Set Web Part Property attributes

    Most attributes are members of the System.ComponentModel namespace. This namespace provides the classes that are used to implement the run time and design time behavior for Microsoft .NET components and controls. At the same time, there are properties like WebPartStorage attribute, which are specifically designed for Web Part properties. The attributes that you specify for the custom property will determine the behavior of the Web Part in the property pane and determine the storage for the custom property. Following is a list of attributes available and how they affect the behavior of the property.

    • Browsable
      Determines whether or not the property is visible in the property pane or not. Setting this attribute to true will include the property.

    • Category
      The property pane is divided into sections. This attribute determines the name of the section in the property pane where the custom property will be displayed. This allows the developer to categories their custom properties according to their logical functionality. If the developer does not specify this attribute or select "Default" as the category, the custom properties will display in Miscellaneous section of the property pane. At the same time, if you select one of the default categories, such as Appearance, Layout or Advanced, the category attribute setting will be ignored and the property will displayed in Miscellaneous section.

    • Default Value
      This is the default value of the property. It will minimize the storage requirement by storing the property value only if it is different from the default value.

    • Description
      Additional information regarding the property for the end-user. The description appears as the tool tip when the mouse hovers over the property.

    • FriendlyNameAttribute
      This is the caption of the property when it is displayed in the property pages. If this attribute is omitted, the actual name of the property is used instead.

    • ReadOnly
      The custom property will be read only if this is set to true.

    • WebPartStorage
      There are three settings for this attribute.
      • Storage.Shared: Display in property pane when the user is in shared view of the page.
      • Storage.Personal: Displays when the user is in shared OR personal view of the page.
      • Storage.None: Does not persist the setting of the property and will not display in the property pane.

    Properties will display in different formats according to the property type. (as displayed below)

    Property TypeDisplay as
    BoolCheck box
    DateTimeText box
    Enum Dropdown box
    int / stringText box

    Code Example >>

  • Rate This Article
    Not HelpfulMost Helpful
    1 2 3 4 5
    Other Articles
    Apr 27, 2004 - Develop and Customize Web Parts with Custom Tool Parts
    Tool Parts provide an interface for Web Part properties well beyond the capabilities of the default property pane. In this article Gayan Peiris shows how to customize Web Parts with custom Tool Parts.
    [Read This Article]  [Top]
    Apr 7, 2004 - Reusable Components in ASP.NET 2.0, Object Binding and Precompilation
    This article demonstrates how to create a reusable component in ASP.NET 2.0 and then consume it from an ASP.NET page. Also learn how the ObjectDataSource control can be used to directly bind the output of an object to the controls in an ASP.NET page and how precompilation can be used to increase the performance of the Web application and catch compilation errors.
    [Read This Article]  [Top]
    Mar 31, 2004 - Build a Managed BHO and Plug into the Browser
    Browser Helper Objects (BHOs) are COM components that communicate with Internet Explorer to enrich the browsing experience. Michele Leroux Bustamante returns to the world of COM to show you how to build a managed BHO with the help of the .NET Framework's COM interoperability features.
    [Read This Article]  [Top]
    Sep 26, 2003 - Accessing Shared Resources Using ASP.NET
    Accessing shared resources is a challenge for many ASP.NET developers. Tony Arslan explains how a simple serviced component can solve this infamous problem.
    [Read This Article]  [Top]
    Oct 2, 2002 - Function Pointers and COM
    Using callbacks and function pointers in VB can be risky and complicated. Ben Garcia explains his work-around for the function pointer issue he encountered while creating the VB version of his SNMP component.
    [Read This Article]  [Top]
    Sep 4, 2002 - Creating an SNMP Component - Part 2
    In part two of this intriguing article series, Ben Garcia shows how to build an updated and improved SNMP component in VC++ AND VB, and he briefly explains why limitations in VB make VC++ a better language for developing this type of application.
    [Read This Article]  [Top]
    Jul 23, 2002 - Creating an SNMP Component
    Ben Garcia sheds some light on the Simple Network Management Protocol (SNMP). First he provides a history of SNMP, then he dives right into its architecture. Finally, he shows how to build a COM component that communicates with SNMP-enabled devices.
    [Read This Article]  [Top]
    Jun 26, 2002 - Accessing Caller ID from the Web - Part 1
    Paul Apostolos begins his series on using Web services and the MSComm32.OCX component to access caller id information from a Web page. In part 1, learn how to write the Visual Basic program that runs on the server and updates a database with incoming callers.
    [Read This Article]  [Top]
    Nov 20, 2001 - Creating a Server Component with VB - Redesigned - Part 2
    Doug Dean explains different methods of retrieving and manipulating data from a database in a VB DLL so that it is ready to be rendered in a browser.
    [Read This Article]  [Top]
    Aug 28, 2001 - Create Your Own Visual Basic Add-Ins
    In this article, S.S. Ahmed shows you how to create VB add-ins. Programmers always feel that they are short of several features while working with development tools. Since Microsoft made Visual Basic an extensible product, VB developers can create their own features in VB.
    [Read This Article]  [Top]
    Mailing List
    Want to receive email when the next article is published? Just Click Here to sign up.

    Support the Active Server Industry



    JupiterOnlineMedia

    internet.comearthweb.comDevx.commediabistro.comGraphics.com

    Search:

    Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

    Jupitermedia Corporate Info


    Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

    Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

    Solutions
    Whitepapers and eBooks
    Microsoft Article: HyperV-The Killer Feature in WinServer ‘08
    Avaya Article: How to Feed Data into the Avaya Event Processor
    Microsoft Article: Install What You Need with Win Server ‘08
    HP eBook: Putting the Green into IT
    Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
    Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
    Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
    Avaya Article: Setting Up a SIP A/S Development Environment
    IBM Article: How Cool Is Your Data Center?
    Microsoft Article: Managing Virtual Machines with Microsoft System Center
    HP eBook: Storage Networking , Part 1
    Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
    MORE WHITEPAPERS, EBOOKS, AND ARTICLES
    Webcasts
    Intel Video: Are Multi-core Processors Here to Stay?
    On-Demand Webcast: Five Virtualization Trends to Watch
    HP Video: Page Cost Calculator
    Intel Video: APIs for Parallel Programming
    HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
    Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
    MORE WEBCASTS, PODCASTS, AND VIDEOS
    Downloads and eKits
    Sun Download: Solaris 8 Migration Assistant
    Sybase Download: SQL Anywhere Developer Edition
    Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
    Red Gate Download: SQL Compare Pro 6
    Iron Speed Designer Application Generator
    MORE DOWNLOADS, EKITS, AND FREE TRIALS
    Tutorials and Demos
    How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
    eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
    IBM Article: Collaborating in the High-Performance Workplace
    HP Demo: StorageWorks EVA4400
    Microsoft How-to Article: Get Going with Silverlight and Windows Live
    MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES