Posts Tagged ‘Asset Picker field’

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
{
get
{
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)
{
base.OnLoad(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()
{
base.CreateChildControls();
// Add the asset picker when in edit mode.
if (base.ControlMode == SPControlMode.Edit)
{
urlSelector = new AssetUrlSelector();
this.Controls.Add(urlSelector);
}
}

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();
}
base.Render(output);
}
}

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.

The AssetUrlSelector control provides much of the functionality that is necessary for the user to browse and select a document in an Office SharePoint Server library. When you add an instance of this control, a text box and browse button are displayed on the page.
Asset Selector
When users click the Browse button, the Asset Picker dialog box appears and lets the user browse the SharePoint Server libraries and select the related file. The URL to the selected file is automatically set on the AssetUrl property file and appears in the text box.

You might want to scope the URL that is selected with the AssetUrlSelector. To allow users to select a document or item only within the current site collection, you can set the AllowExternalUrls property of the AssetUrlSelector class to false. To narrow the scope even more so that users can only select an item within the current web or a specific list or library, you can implement custom code to validate the selected URL.
The end result of this example is that the path of the related file is stored in the custom RelationshipField. However, the relationship remains unknown to the related file because its metadata is never updated. You can use event handlers to ensure that the related file is updated appropriately when a relationship is created or modified; however, this can become complex. There may be scenarios in which the related file cannot immediately be updated, such as if the user does not have edit permissions on the related file, or the related file is checked out by another user.
At this point, the example is basically a reimplementation of the Hyperlink column functionality. However, by using the AssetURLSelector control (or Asset Picker) in Office SharePoint Server, you can add a UI for selecting the corresponding document in the relationship.
This example is intended to highlight the Asset Picker and the fact that you can use it in custom field controls. You can extend the example to provide additional useful functionality, such as letting users specify multiple-link relationships.

Happy Coding ..!!

~ Gangadhar Kotu

Advertisements