Joe's profileJoe Shepherd - Adventure...PhotosBlogListsMore Tools Help

Blog


    January 11

    SharePoint WebPart Property Attributes

    I wanted to put this information out there because it was not well published when I was building WebParts for the first time. When you build a WebPart in MOSS 2007 you have the ability to expose the public properties of the WebPart in the editor pane of the WebPart page. This allows you to set some administrative values for the WebPart behind the scenes. In order to make this happen you have to tag the property as being Web Browsable.

    Unfortunately most of the documentation only gives information on the WebBrowsable and Personalizable attributes. However there are a few others that you might find valuable to know about. Some of the other attributes are listed below along with explanations and links to further resources.

    WebBrowsable [WebBrowsable(True)]
    "Indicates whether the designated property of a Web Parts control is displayed in a PropertyGridEditorPart object." (MSDN)
    http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.webparts.webbrowsableattribute.aspx

    WebPartStorage [WebPartStorage(Storage.Personal)]
    This attribute specifies what type of storage options the WebPart will make use of. The most common is Storage.Personal. "This property can be personalized by individual users. Its WebPartStorageAttribute value is Storage.Personal, which specifies that the property can be stored on a per-user basis. Only users with the Personalize Web Part pages right can set this property." (MSDN)
    http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.webpartpages.webpartstorageattribute.aspx

    Personalizable [Personalizable(true)]
    Allows users the ability to personalize settings for the WebPart.
    http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.webparts.personalizableattribute.aspx

    WebDispayName [WebDisplayName(string)]
    Defines the Friendly Name for a property of a WebPart control. This is the name that will show up in the editor screen.
    http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.webparts.webdisplaynameattribute.aspx

    WebDescription [WebDescription(string)]
    Defines the string value to use as a ToolTip for a property of a Web Parts control. (MSDN)
    http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.webparts.webdescriptionattribute.aspx

    SPWebCategoryName [SPWebCategoryName(string)]
    Defines the friendly or localized name of the category of a property in the CustomPropertyToolPartcontrol inside the ToolPane.
    http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.webpartpages.spwebcategorynameattribute.aspx

    ConnectionProvider [ConnectionProvider(string)]
    Identifies the callback method in a server control acting as the provider in a Web Parts connection, and enables developers to specify details about the provider's connection point. (MSDN) This is used to create connectable WebParts.
    http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.webparts.connectionproviderattribute.aspx

    ConnectionConsumer [ConnectionConsumer(string)]
    Identifies the callback method in a server control acting as the consumer in a Web Parts connection, and enables developers to specify details about the consumer's connection point. (MSDN) This is used to create connectable WebParts.
    http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.webparts.connectionconsumerattribute.aspx

    Below is an example of how to use these attributes in your WebPart code.

    [WebBrowsable(true),
    Personalizable(false),
    WebPartStorage(Storage.Personal),
    WebDisplayName("User Name(Domain\\username)"),
    WebDescription("User to display in the WebPart."),
    SPWebCategoryName("Options")]
    public string UserLoginName
    {
        get { return _loginName; }
        set { _loginName = value; }
    }

    Happy coding!

    Comments (6)

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.
    Joe Shepherd has turned off comments on this page.
    Joe Shepherdwrote:
    Could you provide a litle more detail about what you mean by making a list available for editing? Also the SmartPart will allow you to load a custom user control inside a generic webpart. I have personnaly never used it so I am not sure how it would react.
    May 20
    Rogerwrote:
    Works For Me.  Except:
    My Enum is showing as a text box (might be due to myworking via SmartPart)
    How do you get a list to be available for editing?
    May 20
    Peterwrote:
    Never mind; it looks like that's actually done in the Web Part Gallery itself.
    Apr. 9
    Peterwrote:
    Joe,

        Thanks for the reference.  I have all my namespaces intact and most attributes work (the webpart has been deployed successfully on several builds), but I get Leo's error when I try to add SPWebCategoryName, WebDescription, or WebDisplayName to the class itself.  How can I alter the name, category, and description as shown when selecting web parts in the Web Part Gallery?

    Cheers,

    Peter
    Apr. 9
    Joe Shepherdwrote:
    Leo,
     
    Make sure ythe correct reference set up in the using statements. i.e. System.Web.UI.WebControls.WebParts
    namespace. It is where the WebBrowsable attribute class lives.
     
    Joe
    Mar. 17
    Leowrote:
    Not sure why I kept getting this error when I try to add the attributes.
     
    Error 1 Attribute 'WebBrowsable' is not valid on this declaration type. It is valid on 'property, indexer' declarations only. C:\Documents and Settings\spsdevadm\My Documents\Visual Studio 2005\Projects\TestWebPart\TestWebPart\TestWebPart\TestWebPart.cs 21 10 TestWebPart
    Mar. 17

    Trackbacks (1)

    The trackback URL for this entry is:
    http://joeshepherd.spaces.live.com/blog/cns!9AE2097A4A610B63!302.trak
    Weblogs that reference this entry