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:

[Category("User Control")]
[FriendlyName("User Control (.ascx)")]
[Description("Location of the SearchFormControl.ascx file")]
public string
get{return this
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>"),
public class SearchForm : Microsoft.SharePoint.WebPartPages.WebPart {}

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

<UserControl xmlns="">URI</UserControl>

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


Comments have been disabled for this content.