2

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?

1
  • 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?
    – SPArcheon
    Jul 16, 2013 at 8:09

1 Answer 1

0

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

7
  • 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.
    – SPArcheon
    Jul 16, 2013 at 8:17
  • is the ExtendedPioplePicker correct ?
    – Pushpendra
    Jul 16, 2013 at 8:37
  • Try to use SPFieldType Ex... SPFieldDateTime fldDob = (SPFieldDateTime)lst.Fields.CreateNewField( SPFieldType.DateTime.ToString(), "Dob");
    – Pushpendra
    Jul 16, 2013 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.
    – SPArcheon
    Jul 16, 2013 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, 2013 at 9:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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