5

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.

5

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

8

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;
             }
          }
  • 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
  • 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
  • I will try to find a moment to include the example of the Microsoft.SharePoint.WebPartPages property property. But for the moment, here there is an example: msdn.microsoft.com/en-us/library/… – Bargant Sep 12 '11 at 7:43
2

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?:-)

  • 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
1

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

  • 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
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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