Thursday, March 15, 2007

Where are my Custom Properties?

So you've been doing web part development on SPS2003, you've built web parts with custom properties, which are found when you switch to edit page, edit web part properties. All well and good. Now along comes WSS v3 which use ASP.Net 2.0 web parts as well as SharePoint web parts.

You install the VS.Net 2005 extensions for WSS, you build the sample web part project as described in MSDN, you see that indeed custom properties are supported, cool.

So now you go and copy over some code from your SPS 2003 web parts, you've got the code to add in your custom property:

[Browsable(true), Category("Miscellaneous"), DefaultValue(defaultText), WebPartStorage(Storage.Personal), FriendlyName("Text"), Description("Text Property")]
public string Text
{
get { return text; }
set { text = value; }
}

But when you go to edit your web part properties - this custom property isn't there!!

Here's the catch. From the example in the SDK, the custom property isn't really visible to the ASP.Net web part - instead, the HTMLTextbox control is added to the web part, with an HTMLButton used to call the SaveProperties method.

All well and good, but to me this means my end users can see all of those ugly properties! If the properties are only visible on the Web Part Properties page, using the new security trimming the end user wouldn't be able to see the properties. I'm not so concerned about securing the values of the properties, I just don't want to clutter up my web pages with all these text boxes!

I suppose I could add in code to check the user's security and use CSS to hide the properties, but then that's a performance hit.

The answer: go back to good ol Microsoft.SharePoint.WebPartPages.WebPart instead of System.Web.UI.WebControls.WebParts.WebPart. That is - instead of using the ASP.Net web part base class, you'll need to use the SharePoint web part base class. You can see this yourself if you switch the base class of that SDK sample - now, a Miscellaneous section will appear on the web part properties because the web part is a SharePoint web part, not an ASP.Net web part.

You might also get an error if you try to add a web part that was originally an ASP.Net web part that you re-based the class to become a SharePoint web part...looks like the answer, unfortunately, is to create a brand new project using these instructions:

Creating a SharePoint Web Part


I've only tried this on WSS, but that's sure what seems to be going on...

2 comments:

Anonymous said...

Thank you, man! I thought I was losing my mind there for a moment.

Ralpb said...

After serveral of hours seaching for an solution for this problem, I found this article. It solved my problem right away.