1

I have a web part that has to does something specific with a list. I would like to have users specify which list they want the web part to use when they add it to a page and not worry about it again.

Is there a way to have users set some default values when a web part is added to a page?

Update

I found this post:

http://blog.concurrency.com/sharepoint/create-a-custom-web-part-for-sharepoint-2010/comment-page-1/#comment-1851

Which almost does what I need (which is load a drop down list with the contents of a list), but it does not work properly. I have completed all steps and nothing shows up when I go to "Edit Web Part". Does anyone see anything wrong with his instructions?

3 Answers 3

3

The short answer is 'no. Adding a WebPart to the page does not show a property configuration pane by default. You could however build this yourself.

  • You could render your Web Part differently when it is not configured yet. You could for instance render some of the properties you would normally put in the Web Part Editor panel.

  • You could force the Web Part editor panel to show when adding the webpart to the page. I believe there is some ancient JavaScript you can call to show the panel.

  • You could use the WebPartAdded event on the WebPartManager to plug in some custom UI to edit the just added Web Part. This would only work when you have more control over the page than a single Web Part.

So, a few options which are atainable. Most people choose to do nothing though and have the user manually select the Web part and choose 'Web Part Properties' to show the task pane.

Wouter

1

You can use WebPart properties for this. I have written a blog post about it which can get you started. You can also set a default value.

Webpart Development part 4: Creating Custom Web Part Properties

If you don't want to read my blog post. Google "Custom WebPart property" and you will get lots of good tutorials. :-)

2
  • this is very close to what I need, but it needs to load the DropDownList with the contents of a list. Do you know how this could be done? I found a post that claimed to have a solution (see my update) but it did not work. Apr 13, 2011 at 6:03
  • Hi Abe, Are you looking for a solution based on custom webpart properties or custom editorpart properties? Apr 24, 2011 at 14:33
1

If web parts need end user (designer) configuration I always include a label with a "click here to configure" link in the RenderContents() (or CreateChildControls if you want to control it by adding controls) method if the web part isnt configured:

string errorMsg = (SPContext.Current.FormContext.FormMode == SPControlMode.Display) ? "Mandatory web part properties not set. Edit page to finish configuration of web part." : string.Format("Mandatory web part properties not set. <a id='MsoFrameworkToolpartDefmsg_{0}' href=\"javascript:MSOTlPn_ShowToolPane2Wrapper('Edit','129','{0}');\">Open the tool pane to configure web part.</a>", this.ID);

If you want to add any out of the ordinary (ordinary being checkboxes, textboxes etc that are "translated" automatically from your data types of your web part properties) you need to create a custom edit part. Wictor Wilén has alot of excellent guides for building web parts (we also recently wrote an excellent book on the subject that i can only recommend). Check out this post on building custom edit parts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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