Tuesday, November 27, 2007

Public Property Objects in .Net 2.0/Sharepoint Web Parts (Other than string, bool, int &enum)

I were working on a Custom Web Part for a customer, when I came to the point where I needed to save custom properties in a Web Part, besides the more common ones like string, bool, int and enum.

Theese properties was not to be exposed to the user in the property-pane, but merely to be saved for other purposes.

In my case I needed to save values to an ArrayList and/or a HashTable.

The trick is to define the property as follows:

private ArrayList _myProperties = new ArrayList();

[WebBrowsable(false), Personalizable(true)]

public ArrayList MyProperties
        {
            get
            {
                return this._myProperties;
            }
            set
            {
                this._myProperties = value;
            }
        }

When this is done, you can simply save values to the object from your custom EditorPart (ToolPart), and access it from the Host Web Part.

No comments:

Business Card