Friday, September 18, 2009

Asset Picker field type

In one of my project, I had to create a field type in MOSS with Asset picker functionality. Custom field types are very useful when you are creating custom functionality in MOSS 2007. The following example shows how to provide a functionality to select the document/list item/ from the site. The would be helpful to the user selecting the URL rather typing everything (there is a possibility of typos :) ). We are going to use 'AssetUrlSelector' class from Microsoft.SharePoint.Publishing.WebControls namespace for the purpose.

Create a custom field class by inheriting from SPFieldText class.

public class RelationshipField : SPFieldText {
// Methods

public RelationshipField(SPFieldCollection fields, string fieldName) : base(fields, fieldName) {}

public RelationshipField(SPFieldCollection fields, string typeName, string displayName) : base(fields, typeName, displayName)


// Properties

public override BaseFieldControl FieldRenderingControl




BaseFieldControl control = new RelationshipFieldControl();

control.FieldName = base.InternalName;

return control;




Create a control class by inherting from BaseFieldControl class. and also need to include the following namespaces.

using Microsoft.SharePoint;

using Microsoft.SharePoint.Publishing.WebControls;

using Microsoft.SharePoint.WebControls;

Here is how the control creation goes:

public class RelationshipFieldControl : BaseFieldControl


protected AssetUrlSelector urlSelector;
protected override void OnLoad(EventArgs e)


// Set the value if this is a postback.

if (this.Page.IsPostBack && base.ControlMode == SPControlMode.Edit)


this.ListItemFieldValue = urlSelector.AssetUrl;


protected override void CreateChildControls()


// Add the asset picker when in edit mode.

if (base.ControlMode == SPControlMode.Edit)


urlSelector = new AssetUrlSelector();



protected override void Render(HtmlTextWriter output)

// If this is edit mode and has a value, set the picker.

if (base.ControlMode.Equals(SPControlMode.Edit)

&& this.ListItemFieldValue != null)


this.urlSelector.AssetUrl = this.ListItemFieldValue.ToString();





To register the custom field created above, you need to create a field type definition file as show below. And this xml file needs to be copied to 12\TEMPLATE\XML directory to recognize by MOSS.







Happy Coding... !!

~ Gangadhar Kotu

No comments: