i have this solution deployed on my sharepoint.


And i created a Site column of type Lookup with picker that is from the above solution. Everything works good, the problem is when i try to create the field using code i cannot seem to find a way.

Right now i have the site column create but before i do any opperation on the list item i want to check if it has the column of type lookup with picker if so proceed and if not create it.

Here is what i got so far.

if (parentSite.Fields.TryGetFieldByStaticName("TestColumn") == null)

    parentSite.Fields.Add("TestColumn", !myCustomType!, true);

Help :'(

  • I found a faulty way, having the getting another field with the same type and using its FieldType attribute, but it doesnt fix the problem. I need to be able to list every FieldTypes on the site. Oct 29, 2012 at 20:34
  • Are you writing this code in Feature Activated event? Oct 30, 2012 at 6:28
  • No, its in a webpart. Oct 30, 2012 at 13:10
  • ok can you provide some error that you are getting? Oct 30, 2012 at 14:14
  • Its not an error, I cannot give the function Add a correct SPFieldType because the fieldType i want to create is deployed from another solution. Therefore it is not in the SPFieldType enum that we usually use. Oct 30, 2012 at 14:54

1 Answer 1


So, it took me over 1 hour fighting with this, however it isn't fully solved. Firstly, you are working with Lookup. These fields are really pain...

You cannot so simply add a lookup field to site, also, from my personal experience - lookups as site fields are not a good solution. SPFieldCollection has overloaded method Add(), which require only SPField. However, this one didn't work correctly for me.

Retrieving custom field, easy way: Just add reference to LookupFieldWithPicker.dll, you can find it in the wsp package. Retrieving custom field using Reflection:

var assembly = Assembly.Load("iLoveSharePoint.Fields.LookupFieldWithPicker, Version=, Culture=neutral, PublicKeyToken=3443098eba475529");
var fieldType = assembly.GetTypes().FirstOrDefault(type => type.Name == "LookupFieldWithPicker");
// just use SPFieldLookup constructors, below overload nr 3
var field = Activator.CreateInstance(fieldType, web.Fields, fieldType.Name, "instanceName") as SPFieldLookup;

From now, you must create lookup using AddLookup method, because other one fails.

// throws: SPException: 'Use AddLookup instead'. Nice to know... 
//var name = web.Fields.Add("webLookupName", SPFieldType.Lookup, true);

var name = web.Fields.AddLookup("webLookupName", list.ID, true);

// redundant, just checking if field exist
var test = web.Fields.TryGetFieldByStaticName(name);

var field = new LookupFieldWithPicker(web.Fields, name);
field.StaticName = "IhaveChangedYourName";

// probably redudant

I finished on this. It is adding correctly lookup AND set staticName to "Ihave(...)" (check it with sharepoint manager). However, it is not changing the field type correctly. Potencial solution to resolve this problem? Check this blog. Just modify the field XML. Should work, however this is brute force.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.