Ad

Get A Reference To Current Listitem From Within Custom SPField

- 1 answer

I've successfully created a custom field inheriting from SPFieldText and happily have complete control over rendering it on the input form as a control.

Problem:

I have a need to create a link to a popup with the ListId and ListitemID in the querystring when rendering the field using GetFieldValueAsHtml().

Something like this:

public class CustomField : SPFieldText
{
    public CustomField (SPFieldCollection fields, string fieldName)
        : base(fields, fieldName)
    {
    }

    public CustomField (SPFieldCollection fields, string typeName, string displayName)
        : base(fields, typeName, displayName)
    {
    }

    public override string GetFieldValueAsHtml(object value)
    {
        return string.Format(
            "javascript:window.open('{0}/_layouts/Popup.aspx?ListId={1}&ItemId={2}','Popup','status=0,scrollbars=0,titlebar=0,resizable=1,toolbar=0,location=0,width=600,height=500');return false;",
            SPContext.Current.Web.ServerRelativeUrl.TrimEnd('/'), 
            LISTID, LISTITEM.ID
            );
     }

Clearly SPContext doesn't hold a reference to the list or item and none of the properties seem to expose the current item. I tried overloading properties in the control but these don't seem to be invoked when rendering the field.

// None of these properties are invoked when rendering the field as above    
public class CustomFieldControl : TextField
{
     public override object ItemFieldValue
     public override object ListItemFieldValue
     public override string Text
     public override object Value
}

I've experimented with the RenderPattern in fldtypes_Custom.xml but again this is also ignored when rendering the field using GetFieldValueAsHtml();

Am I naively expecting something that's not possible? I'm open to any approach that avoids rewriting the web part... or just tell me it can't be done.

(The existing web part renders a grid and calls GetFieldValueAsHtml(). We know we can change the web part to achieve this but that's not an ideal solution for other reasons).

Ad

Answer

For anyone stumbling across this, I confirmed that what I was aiming to do is not possible.

We were forced to make changes in the web part to achieve this level of customization. As outlined in the question, The existing web part renders a grid and calls GetFieldValueAsHtml().

Ad
source: stackoverflow.com
Ad