Displaying custom SharePoint webpart property error message in tool pane

When creating webpart with properties in them to store custom data – you can never rely too much on users entering proper data. You definitely need proper error handling and reporting, and here is what this post is about. You can create custom label on your webpart field ,to report errors to users but in this post we’ll talk about error reporting to priledged users that modify webpart properties.

The best approach in this case is to set a handler on your SET public property to validate the value the user is tring to enter and return an error or persist the value.

Something like this (where I validate email address with my ValidEmail custom function):

string formRecipient;
        [
            Personalizable(PersonalizationScope.Shared),
            WebBrowsable(true),
            WebDisplayName("Form Recipient Email"),
            WebDescription("Email address of the complaint administrator")
        ]
        public string FormRecipient
        {
            get { return formRecipient; }
            set
            {
                if (!ValidEmail(value))
                {
                    throw new WebPartPageUserException(“Invalid ‘Form Recipient’ email address. Only one valid email address permitted by the system.”);
                }
                else
                {
                    formRecipient = value;
                }
            }
        }

In here WebPartPageUserException  will ensure your exception message is displayed on the top of the webpart tool pane and properties are not saved.

This entry was posted in MOSS, sharepoint and tagged , , , . Bookmark the permalink.

Comments are closed.