locked
Changing Lookup field behaviour RRS feed

  • Question

  • Hello,

    By default, Sharepoint renders dropdown lookup fields as html <select> elements if the control holds less than 20 items, or as html <input> elements if the control holds more than 20 items.

    I would like to change this so that these controls are always rendered in html <input> fields. I am interested in having the "type-and-filter" javascript functionality which only the extended lookup field has, as a html <input> element, and have it (if possible) changed for all lookup fields on all libraries, lists and sites.

    Based on my knowledge, theoretically this can be achieved in two ways:

    1) Overwrite the default .ascx template for the single lookup with the one used for the multiple lookup.
    2) Patch Microsoft.Sharepoint.dll to alter the threshold from 20 to 1, for example.

    Does anybody know where can I find the html templates for these two controls, so that point 1) would be put to practical use?
    I'm not really keen on patching the dll for all the possible reasons, so 2) is rulled out, I guess...

    Or is there another way?

    Thanks,
    Tudor Olariu
    Thursday, October 25, 2007 1:25 PM

Answers

  • The control templates can be found in C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\CONTROLTEMPLATES.

     

    The ones you are interested in I think are:

     

    InputFormControl.asvx: This seems to be the template for rendering input controls in forms, but the meat you want seems to be handled by code that populates this tag: <aspStick out tonguelaceholder ID="PlaceHolderControl" runat="server" />.

    DefaultTemplates.ascx: This looked like what you are looking for, as it contains editable fields liek TextField in addition to some other tempaltes used throughout SharePoint. Look for the template ID MultipleLookupField, but I did not see one here for LookupField.

    LookupFieldEditor.ascx: the template used for the custom properties of a lookup field when you are adding/editing it within the List Settings.

    UserFieldEditor.ascx: Same as LookupFieldEditor.ascx but for user lookups.

     

    I recommend that instead of changing the ones for Out of the Box SharePoint, you consider creating your own custom site columns that are based on the standard field types. You can add your own FIELDTYPES_MyName.xml file to the C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\XML folder to specify your own types that inherit from the base types, and that will also allow you to point to your own copies of the ASCX files. Of course, I guess this won't satisfy your users if they have already begun using the standard lookups, but it would be better to do if the SharePoint deployment is new.

     

    Actually, digging deeper I see what you want is acutally in the FIELDTYPES.XML file.

     

    Search for "<Field Name="TypeName">Lookup</Field>"

    Then look under the RenderPattern tag for the edit pattern.

    <RenderPattern Name="EditPattern">
    <RenderPattern Name="PreviewEditPattern">

     

    But looking at the CAML it appears it always renders as OPTION/SELECT and never INPUT tags. Perhaps you will find that it uses a different template when the count is over 20?

    Thursday, October 25, 2007 3:28 PM

All replies

  • The control templates can be found in C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\CONTROLTEMPLATES.

     

    The ones you are interested in I think are:

     

    InputFormControl.asvx: This seems to be the template for rendering input controls in forms, but the meat you want seems to be handled by code that populates this tag: <aspStick out tonguelaceholder ID="PlaceHolderControl" runat="server" />.

    DefaultTemplates.ascx: This looked like what you are looking for, as it contains editable fields liek TextField in addition to some other tempaltes used throughout SharePoint. Look for the template ID MultipleLookupField, but I did not see one here for LookupField.

    LookupFieldEditor.ascx: the template used for the custom properties of a lookup field when you are adding/editing it within the List Settings.

    UserFieldEditor.ascx: Same as LookupFieldEditor.ascx but for user lookups.

     

    I recommend that instead of changing the ones for Out of the Box SharePoint, you consider creating your own custom site columns that are based on the standard field types. You can add your own FIELDTYPES_MyName.xml file to the C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\XML folder to specify your own types that inherit from the base types, and that will also allow you to point to your own copies of the ASCX files. Of course, I guess this won't satisfy your users if they have already begun using the standard lookups, but it would be better to do if the SharePoint deployment is new.

     

    Actually, digging deeper I see what you want is acutally in the FIELDTYPES.XML file.

     

    Search for "<Field Name="TypeName">Lookup</Field>"

    Then look under the RenderPattern tag for the edit pattern.

    <RenderPattern Name="EditPattern">
    <RenderPattern Name="PreviewEditPattern">

     

    But looking at the CAML it appears it always renders as OPTION/SELECT and never INPUT tags. Perhaps you will find that it uses a different template when the count is over 20?

    Thursday, October 25, 2007 3:28 PM
  • Hi All,

     

    I've been following this thread with some interest since I'm hacking at the SharePoint LookupField implementation, trying to create my own custom filtered lookup field. It seems that SP hardcodes the 20 item limit. Here is the code from Reflector:

     

        class LookupField: BaseFieldControl
        {

     ...

            protected override void CreateChildControls()
            {
                if (!base.IsFieldValueCached)
                {
                    if (base.Field == null)
                    {
                        return;
                    }
                    base.CreateChildControls();
                    if (base.ControlMode == SPControlMode.Display)
                    {
                        return;
                    }
                    SPFieldLookup field = (SPFieldLookup)base.Field;
                    if (field.AllowMultipleValues)
                    {
                        return;
                    }
                    this.Controls.Clear();
                    if ((((this.DataSource != null) && (this.DataSource.Count > 20)) && (!base.InDesign && SPUtility.IsIE55Up(this.Page.Request))) && !SPUtility.IsAccessibilityMode(this.Page.Request))
                    {
                        this.m_tbx = new TextBox();
                        this.m_tbx.Attributes.Add("choices", this.Choices);
                        this.m_tbx.Attributes.Add("match", "");
                        this.m_tbx.Attributes.Add("onkeydown", "HandleKey()");
                        this.m_tbx.Attributes.Add("onkeypress", "HandleChar()");
                        this.m_tbx.Attributes.Add("onfocusout", "HandleLoseFocus()");
                        this.m_tbx.Attributes.Add("onchange", "HandleChange()");
                        this.m_tbx.Attributes.Add("class", "ms-lookuptypeintextbox");
                        this.m_tbx.Attributes.Add("title", field.Title);
                        this.m_tbx.TabIndex = this.TabIndex;
                        this.m_tbx.Attributes["optHid"] = this.HiddenFieldName;
                        Literal child = new Literal();
                        child.Text = "<span style=\"vertical-align:middle\">";
                        Literal literal2 = new Literal();
                        literal2.Text = "</span>";
                        this.Controls.Add(child);
                        this.Controls.Add(this.m_tbx);
                        this.m_tbx.Attributes.Add("opt", "_Select");
                        this.m_dropImage = new Image();
                        this.m_dropImage.ImageUrl = "/_layouts/images/dropdown.gif";
                        this.m_dropImage.Attributes.Add("alt", SPResource.GetString("LookupWordWheelDropdownAlt", new object[0]));
                        this.m_dropImage.Attributes.Add("style", "vertical-align:middle;");
                        this.Controls.Add(this.m_dropImage);
                        this.Controls.Add(literal2);
                    }
                    else
                    {
                        this.m_dropList = new DropDownList();
                        this.m_dropList.ID = "Lookup";
                        this.m_dropList.TabIndex = this.TabIndex;
                        this.m_dropList.DataSource = this.DataSource;
                        this.m_dropList.DataValueField = "ValueField";
                        this.m_dropList.DataTextField = "TextField";
                        this.m_dropList.ToolTip = SPHttpUtility.NoEncode(field.Title);
                        this.m_dropList.DataBind();
                        this.Controls.Add(this.m_dropList);
                    }
                }
                else
                {
                    base.CreateChildControls();
                    return;
                }
                if (this.m_webForeign != null)
                {
                    this.m_webForeign.Close();
                    this.m_webForeign = null;
                }
                this.Controls.Add(new LiteralControl("<br/>"));
                this.SetFieldControlValue(this.ItemFieldValue);
            }

     ...

    }

     

    Looks like there's no configuration parameter for this ...

     

    Regards

    Wong Y-Mi

     

     

     

    • Proposed as answer by Shegit Brahm Monday, January 9, 2012 12:22 PM
    Wednesday, March 5, 2008 9:11 AM
  • Hello?

    Is there any Microsoft support engineers that monitor this thread - any thread in this forum?  Why is there no answer to the comments regarding this hard limit of 20 - positively or negatively - by Microsoft?  This is Microsoft TechNet Forums is it not?
    Dodge
    Thursday, March 5, 2009 3:47 PM
  • Dodge - from what I've seen - they only step in when you've posted your question under the wrong category :P

    here is a pretty cool set of posts that will (MAYBE) help clarify

    Cascading Drop downs  --Complex - handles the list of 20+)
    http://mdasblog.wordpress.com/2009/07/14/cascading-columns-in-a-sharepoint-form/

    Simplified not 20+ stuff
    http://mdasblog.wordpress.com/2009/07/19/cascading-dropdown-columns-in-a-sharepoint-form-part-2/

    Demo  http://www.sympraxisconsulting.com/Demos/Demo%20Pages/CascadingDropdowns.aspx

    the reflected code tells alot too - thanks for that wym

    Friday, July 31, 2009 7:57 PM
  • All:

    I'm glad you ran across my blog posts above, and I hope they were helpful. Let me know if you have any questions I can help with.

    I also wanted to let you know that I've been working on a jQuery library for SharePoint Web Services that now has cascading dropdowns built into it.  Right now, it only supports dropdowns with <20 options, but we'll get the more complicated logic for 20+ in soon.  You can check it out here:
    http://spservices.codeplex.com/

    M.
    Marc D Anderson - Sympraxis Consulting LLC - Marc D Anderson's Blog - @sympmarc
    Friday, August 28, 2009 3:21 AM