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=18.104.22.168, 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.