Setting custom WebPart Properties in the dwp file

I've been developing webparts that use regular ASP.NET Usercontrols as content for a while. Up until now we've simply hardcoded the url to the usercontrol location in the webpart class. Moving towards a more standardized product that must support differentiated deployment envrironments I wanted to place the url to the usercontrol in the webpart dwp file.

The docs availible online describes how to annotate the property to read from the dwp file, much like this:

[Browsable(true)]
[Category("User Control")]
[DefaultValue(_defaultUserControl)]
[WebPartStorage(Storage.Shared)]
[FriendlyName("User Control (.ascx)")]
[Description("Location of the SearchFormControl.ascx file")]
public string
UserControl
{
get{return this
._userControl;}
set{_userControl = value
;}
}

Then you see this in the dwp file template:

< ! - Specify initial values for any additional base class or custom properties here. -->

Well, simply adding an element with the same name as the property simply won't work. Some additional guidance from Redmond Sharepoint team-guy Scott pointed out the missing link:

You've got to annotate the webpartclass with a reasonable namespace like this:

[DefaultProperty("Text"), ToolboxData("<{0}:SearchForm runat=server></{0}:SearchForm>"),
XmlRoot(Namespace="
http://www.objectware.no/Lawyer/Sharepoint/Webparts/SearchForm"
)]
public class SearchForm : Microsoft.SharePoint.WebPartPages.WebPart {}

And then tag your property elements in the dwp file with the same xmlns:

<UserControl xmlns="http://www.objectware.no/Lawyer/Sharepoint/Webparts/SearchForm">URI</UserControl>

And your webpart has an initial propertyvalue after deployment. Thanks Scott!

Filed under:

Comments

# Travis said:

Awesome. I couldn't figure it out either. Thanks.

Friday, June 18, 2004 1:20 PM
# roulio said:

I don't want to declare the URL http://www.objectware.no/Lawyer/Sharepoint/Webparts/SearchForm but as soon as i try anything else it doesn't work
What migth I have to define to have my owm Namespace?
Need help about this...
Thanks by advance

Monday, July 26, 2004 6:05 AM
# TrackBack said:
Tuesday, March 15, 2005 6:17 AM
# TrackBack said:
Thursday, March 17, 2005 10:58 PM
# TrackBack said:

<p>&lt;ul&gt;&lt;li&gt;&lt;a href=&quot;http://weblogs.asp.net/mnissen/archive/2004/05/20/135744.aspx&quot; target=&quot;_blank&quot;&gt;Setting custom WebPart Properties in the dwp file&lt;/a&gt; &lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://mtaulty.com/b

Thursday, March 24, 2005 3:13 AM
# TrackBack said:
Monday, June 06, 2005 11:51 PM
# Neha said:

How to apply validation for custom properties?

Friday, March 28, 2008 3:36 AM
# User Control Container Web Part « Sharepoint Musing’s said:

Pingback from  User Control Container Web Part &laquo; Sharepoint Musing&#8217;s

Tuesday, July 08, 2008 7:35 AM
# Lipitor side effects. said:

Lipitor unusual side effect. Lipitor when genaric. Is lipitor best for brain lesions. Generic version of lipitor. Lipitor banned. Lipitor.

Friday, July 11, 2008 9:05 AM
# Lexapro. said:

Lexapro. Gain weight on lexapro. Lexapro and alcohol use.

Saturday, July 19, 2008 6:41 PM
# User Control Container Web Part « Sharepoint Musing’s said:

Pingback from  User Control Container Web Part &laquo; Sharepoint Musing&#8217;s

Tuesday, July 22, 2008 4:57 AM

Leave a Comment

(required) 
(required) 
(optional)
(required)