Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

A custom web part has custom web part properties. I would like to validate with custom server side code when user changes property value (or when properties are bing saved). Is that possible? Can I show an error message in the tool pane describing the problem that occurred?

Example: I have a web part where user needs to enter a list name (in a property). I would like to check if list with such a name exists.

share|improve this question

5 Answers 5

up vote 5 down vote accepted

you could create a custom EditorPart which creates a custom interface in which you verify the input. Check this out: Web Part Properties - part 2 - Editor Parts

share|improve this answer

I hope this can help you, althought I have not tested it. You can find an example here: http://www.sharepoint-tips.com/2010/06/validating-web-part-properties.html

private string _webPartContentLink = null;
 [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(true)]
        [System.ComponentModel.Category("Services")]
        [WebDisplayName("Enter WebPart Content Link")]
        [WebDescription("Enter WebPart Content Link")]
        public string WebPartContentLink 
        {
            get
            {
                if (_webPartContentLink == null)
                {
                    _webPartContentLink = string.Empty;
                }
                return _webPartContentLink;
            }
            set 
            {
                if (!value.StartsWith("http://"))
                      throw new WebPartPageUserException("The WebPart Content Link is not valid");
                }

                _webPartContentLink = value;
             }
          }
share|improve this answer
2  
Should change Exception to WebPartPageUserException. If I remember correctly Exception will generate a default value for the user. –  Anders Aune Sep 7 '11 at 9:40
    
Sure, I caught the first example in the page. I'll edit it now. Please vote if you like my answer, I need some reputation :) –  Bargant Sep 7 '11 at 10:13
2  
1+ since I can not accept yours as answer –  Abu Hamzah Sep 7 '11 at 14:49
    
Thanks @Abu Hamzah. I figured it would be nicer to have all this information in one place. :) –  Kit Menke Sep 7 '11 at 15:01
    
+1 really nice answer! Maybe you can include an example of the Microsoft.SharePoint.WebPartPages property in addition to the System.Web.UI.WebControls you have above. –  Kit Menke Sep 7 '11 at 15:15

In addition to what Wictor said, remember to define your own ValidationGroup when you are using custom or out of the box ASP.NET validators, that way you dont get interference from other validation controls that might be on the same page when you do a post back (maybe a subject for your book Wictor?:-)

share|improve this answer
    
Already in there mate! And some other neat tips and tricks! –  Wictor Wilen MCA MCM MVP Mar 22 '10 at 10:09
    
And what if this web part is part of sandboxed solution? I presume this will not work without a custom proxy? –  Toni Frankola Mar 22 '10 at 12:10
    
No probs Toni, EditorParts work fine within the SB! –  Wictor Wilen MCA MCM MVP Mar 22 '10 at 12:41

Check out the following article: https://www.nothingbutsharepoint.com/sites/devwiki/articles/Pages/SharePoint-Web-Parts-with-jQuery-and-the-Web-ToolPart.aspx

share|improve this answer
    
Welcome to SharePoint Stack Exchange! While this may theoretically answer the question, we prefer inclusion of the essential parts of the answer here, and to provide the link for reference. See How to Answer for general guidelines. –  Alex Angas Aug 25 '11 at 2:28

You could also create a custom editor part with a textbox and ASP.NET validation control, e.g a RegularExpressionValidator. This gives you client-side validation on the property. The only disadvantage I can see with that solution is that the custom editor parts are always displayed in the top (which quite anoying...).

See this MSDN article for a walk-through: http://msdn.microsoft.com/en-us/library/hh228018.aspx#Y4419

share|improve this answer
    
FYI.. I've moved your answer from the duplicate question. –  Kit Menke Sep 7 '11 at 14:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.