How to create a filtered lookup field/column programmatically from this FieldType !? ..using CAML or c#

Sep 26, 2011 at 10:43 PM


I need to add the Filtered Look Up Column to Custom List Definitions or to add it programmatically using c#-Code..

Thanks for helps!

Oct 21, 2011 at 8:23 AM

To add the filtered lookup column into you list definition, you can copy the code that you normally use within a simple lookup field and you have to change the value of the Type attribute to "FilteredLookupField"
Instead, if you want to add the filtered lookup field programmatically, you have to reference to the Dev4Side.SP2010.FilteredLookup.dll file and you have to instantiate manually the type FilteredLookupField.

Hope this helps.

Oct 25, 2011 at 3:28 PM

I tried adding it programmatically but did not work.

The field got created but the Lookup List and field is not set correctly. How can I set it?

Below is the code that I used..


SPFieldLookup filteredLookup = (SPFieldLookup)list.Fields.GetFieldByInternalName(list.Fields.AddLookup("filteredLookup", lookupList.ID, false));

filteredLookup.LookupField = "Title";


filteredLookup.SchemaXml = filteredLookup.SchemaXml.Replace("Lookup", "FilteredLookupField");

filteredLookup.SetCustomProperty("QueryFilterAsString", myCAMLQuery); //this worked..


Oct 25, 2011 at 4:10 PM


The LookupField is expecting the Guid for "Title" and not the name.

But still, when I go to edit page of the field in the UI, the List and Column dropdowns are not showing the correct values.. How can I get that fixed!

Oct 25, 2011 at 4:51 PM

Got that fixed as well!

It was because  listTargetList.Items.FindByValue(TargetListId) was giving null. The reason was the TargetListId was in Uppercase and had { } which didn't match.

Thanks anyways!

Mar 4, 2012 at 1:24 PM

Hi PeppeDotNet, can you clarify two points:

When you say "change the value of the Type attribute to 'FilteredLookupField'", do you mean as Raj shows using the SchemaXml or some other way?

When you say "instantiate manually the type FilteredLookupField", as per the source code the constructor calls the base LookupField constructor which does not create a new field, it merely get a reference to an existing field, which doesn't exist yet.