Logo 
Search:

Sharepoint Forum

Ask Question   UnAnswered
Home » Forum » Sharepoint       RSS Feeds

Programmatic Adding of a Column to Document Library Gives Errors

  Asked By: Jasen    Date: Mar 16    Category: Sharepoint    Views: 5735

I'm adding a column to a document library via code. Note that this is
not a Site Column at the Site Collection level. This is as if I'm going
into the settings of a document library and clicking the Create Column
link in the Columns section of the Settings page.

The code seems to work (all done via a FeatureReceiver) and upon looking
at the document library settings I see the new column. Now, when I try
to upload a piece of content to the library I get an error: "One or more
field types are not installed properly. Go to the list settings page to
delete these fields.". The field that's causing this is the field I'm
adding through code.

Weird observation: if after activating the Feature I go into the
document library's settings, click on the newly added column, and then
just click the <OK> button, and then return to the document library and
upload a piece of content, all works, and the new column appears on the
properties page.

So, I must be missing something in code, but I'm doing .Update where
needed. Can someone help me out with what I'm missing?

See code below...

internalFieldName = docLib.Fields.AddLookup(fieldName, lookupList.ID,
lookupList.ParentWeb.ID, required);
// cast created field to a SPLookup
SPFieldLookup lookup =
(SPFieldLookup)docLib.Fields.GetFieldByInternalName(internalFieldName);
// value additional attributes based on passed parameters
lookup.AllowMultipleValues = allowMultipleValues;
lookup.Description = fieldDescription;
lookup.LookupField = lookupField;
// update the lookup field...this is required whenever modifying
properties of the SPLookup
lookup.Update();
docLib.Update();

Share: 

 

4 Answers Found

 
Answer #1    Answered By: Girja Garg     Answered On: Mar 16

I don't necessarily have an answer for you (yet), but is this Document
Library based  on a site  content type?

 
Answer #2    Answered By: Jarod Mathews     Answered On: Mar 16

After stepping away for a few I
found the issue. Looking at the line of code  below, while I was using
the internalFieldName for the column  I was creating, the name I was
using for the lookupField was the Display Name, and not its internal
one. Once I swapped to the correct field  name it was all good.

lookup.LookupField = lookupField; <-- lookupField must be internal
field name

 
Answer #3    Answered By: Amanda Lewis     Answered On: Mar 16

That one has gotten me a few times as well; next time I'll read the
message more carefully. Thanks for pointing it out, and I'm glad you fixed
your problem.

 




Tagged: