Setting the property ‘AllowMultipleValues’ resets the control’s type to "Lookup" when it is inherited from SPFieldLookup class.


When you develop a custom field type that inherits from SPFieldLookUp/SPFieldUser(not sure about any other controls) .
The derived field type has its own rendering controls. The issue arises when multiple values are to be stored in a field of the custom fieldtype.
In order to save multiple values to an SPFieldLookUp the property "AllowMultipleValues" should be set to true . When "AllowMultipleValues" property is
set to true, the field rendering control is overridden by MultipleLookupField. So the custom field rendering control is never instantiated/render if the field is
set to store multiple values(AllowMultipleValues=true). As a workaround what I did is overridden the ‘OnAdded’ function and edit the schema to append the ‘Mult’ attribute.

   1: using System;
   2: using System.Collections.Generic;
   3: using System.Text;
   4: using Microsoft.SharePoint;
   5: using Microsoft.SharePoint.WebControls;
   6: using System.Web.UI;
   7: using System.Xml;
   8:  
   9: namespace CustLookuColumn
  10: { 
  11:     public class MultiLookUp : SPFieldLookup  
  12:     {        
  13:        
  14:        public MultiLookUp(SPFieldCollection fields, string fieldName)
  15:             : base(fields, fieldName)
  16:         {          
  17:                    
  18:         }
  19:  
  20:        public override void OnAdded(SPAddFieldOptions op)
  21:        {
  22:            base.OnAdded(op);        
  23:            XmlDocument doc = new XmlDocument();
  24:            doc.LoadXml(this.SchemaXml);
  25:            XmlNode element = doc.FirstChild;
  26:            XmlAttribute attr = doc.CreateAttribute("Mult");
  27:            attr.Value = "TRUE";
  28:            element.Attributes.Append(attr);
  29:            this.SchemaXml = doc.OuterXml;
  30:            base.Update();
  31:  
  32:        }
  33:        public MultiLookUp(SPFieldCollection fields, string typeName, string displayName)
  34:             : base(fields, typeName, displayName)
  35:         {
  36:            
  37:         }        
  38:        
  39:         public override BaseFieldControl FieldRenderingControl
  40:         {
  41:             get
  42:             {
  43:                 BaseFieldControl fieldControl = null;
  44:                 fieldControl = new RenderFieldControl(); //See the below code snippet                               
  45:                 fieldControl.FieldName = InternalName;
  46:                 return fieldControl;
  47:             }
  48:         }
  49:     }   
  50: }

 

   1: using System;
   2: using System.Runtime.InteropServices;
   3: using Microsoft.SharePoint;
   4: using Microsoft.SharePoint.WebControls;
   5: using System.Web.UI;
   6: using System.Web.UI.WebControls;
   7: using System.Web.UI.HtmlControls;
   8:  
   9: //This is just a skeleton, I haven't included the entire functionality. 
  10:  
  11: namespace CustLookuColumn
  12: {
  13: public class RenderFieldControl : BaseFieldControl
  14:    {      
  15:        protected override void CreateChildControls()
  16:        {
  17:            base.CreateChildControls();
  18:         }
  19:  
  20:     }
  21:  
  22: }

 

   1: <?xml version="1.0" encoding="utf-8"?>
   2: <FieldTypes>
   3:   <FieldType>
   4:     <Field Name="TypeName">MultiLookUp</Field>
   5:     <Field Name="TypeDisplayName">MultiLookUpField</Field>
   6:     <Field Name="TypeShortDescription">Custom Multi LookUp Field</Field>
   7:     <Field Name="ParentType">LookupMulti</Field>
   8:     <Field Name="UserCreatable">TRUE</Field>
   9:     <Field Name="FieldTypeClass">CustLookuColumn.MultiLookUp,CustLookuColumn , Version=1.1.1.1, Culture=neutral, PublicKeyToken=6332f1922196b6de</Field>
  10:     <Field Name="FieldEditorUserControl">/_controltemplates/YOUR_LookupFieldEditor.ascx</Field>
  11:     <!-- 
  12:         YOUR_LookupFieldEditor.ascx is not included it would be identical to the ascx control under ..\12\TEMPLATE\CONTROLTEMPLATES\LookupFieldEditor.ascx
  13:     -->
  14:   </FieldType>
  15: </FieldTypes>

 

Note: The samples provided within this blog may not work, my intention was to provide you a workaround when you hit with this issue.

Happy customization!!!


Skip to main content