Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

i have this solution deployed on my sharepoint.

http://ilovesharepoint.codeplex.com/releases/view/44989

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);
    parentSite.Update();
}

Help :'(

share|improve this question
    
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. –  Dr.Denis McCracleJizz Oct 29 '12 at 20:34
    
Are you writing this code in Feature Activated event? –  Prashant Lakhlani Oct 30 '12 at 6:28
    
No, its in a webpart. –  Dr.Denis McCracleJizz Oct 30 '12 at 13:10
    
ok can you provide some error that you are getting? –  Prashant Lakhlani Oct 30 '12 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. –  Dr.Denis McCracleJizz Oct 30 '12 at 14:54

1 Answer 1

up vote 2 down vote accepted
+50

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=14.0.0.0, 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);
web.Update();

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

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

// probably redudant
web.Update();

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.

Cheers.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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