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 the necessity to switch from using a default field to using a custom one in a number of list. I have also to "copy" the value contained in the old to-be-deleted column to the new created one. To achieve my goal I have tried a programmatic approach: add my custom field to the list and then copy the value from another, already created field. I had wrote this code:

SPField newField=list.Fields.CreateNewField("ExtendedPioplePicker", field.Title + "_new");
list.Fields.Add(newField);
foreach (SPItem item in list.Items)
{
    var fld = item.Fields.GetField(field.Title + "_new");
    fld.Type = Microsoft.SharePoint.SPFieldType.User;
    var value=item[field.InternalName];
    item[fld.InternalName]=value;
}

But then fld.InternalName throws an ArgumentException. What I need to do for fix problem and update data in field?

share|improve this question
    
So, basically you have to copy the value from the old field to the newly created one. The new field is an "User" field like in the above sample? Also, that is the actual code you are using? –  SPArchaeologist Jul 16 '13 at 8:09
add comment

1 Answer 1

I changed your code a bit...replaced ExtendedPioplePicker by Text and its working fine.

share|improve this answer
    
Yeah, unless the field type isn't actually called "ExtendedPioplePicker" that wouldn't work... the problem is that he probably is trying to create an instance of his custom field, not a new Text/Note one. –  SPArchaeologist Jul 16 '13 at 8:17
    
is the ExtendedPioplePicker correct ? –  Pushpendra Jul 16 '13 at 8:37
    
Try to use SPFieldType Ex... SPFieldDateTime fldDob = (SPFieldDateTime)lst.Fields.CreateNewField( SPFieldType.DateTime.ToString(), "Dob"); –  Pushpendra Jul 16 '13 at 8:43
    
Pushpendra, that would be an idea... but from the original question and the name of the field type he is trying to use I would assume we are talking about a custom field type. That means that you won't find it in the enum. –  SPArchaeologist Jul 16 '13 at 9:00
    
Sorry I diverted you, but the rest part of the code works fine. You got the problem with fld.InternalName which works fine for me. –  Pushpendra Jul 16 '13 at 9:17
show 2 more comments

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.