Mittwoch, 4. April 2012

ArgumentException when setting SPUser to a UserField in SPListItem

A little hint for everyone that ever got an argument exception if he tried to set a SPUser-object to an userfield in a list item:

SPListItem listItem = list.Items.Add(listRootFolder.ServerRelativeUrl, SPFileSystemObjectType.File, null);
listItem["Owner"] = spUser; <-- if you step through the code while debugging, an argument exception may occur here 
listItem["Url"] = url; 
listItem["Title"] = title; 

In case of an argument exception, you have to check if the assigned spUser-object has been opened from the same parent web as the list.