To save a user name into a people field:
Provided a people editor control in my custom form and saved each resolved entity as follows:
if (currentPeopleEditor.Entities.Count != 0)
{
SPFieldUserValueCollection userCollection = new SPFieldUserValueCollection();
for (int index = 0; index < currentPeopleEditor.ResolvedEntities.Count; index++)
{
PickerEntity ObjEntity = (PickerEntity)currentPeopleEditor.ResolvedEntities[index];
userCollection.Add(new SPFieldUserValue(objSPWeb,Convert.ToInt32(ObjEntity.EntityData["SPUserID"]), ObjEntity.Key));
}
newItem[Field.Key.ToString()] = userCollection;
}
It was working very fine until some user stated getting this exception: "Invalid Look-up Value
A look-up field contains invalid data, Please check the value and try again."
On investigating we found that this error was occurring because ObjEntity.EntityData["SPUserID"]) was returning as null.
It was happening because the requirement is to save some users name who don't access this site collection but they are member of corporate AD system.