Tuesday, February 02, 2010

Microsoft SharePoint 2007 – ItemAdded/ItemAdding List Event Receivers and SPFieldUserValue.User not returning SPUser

 

The Problem
I recently had to develop an Event Receiver that would require reading off the SPUser object of the item. What I found is that, at times when the exact name of the user is entered (and you click OK on the NewForm.aspx page without clicking the check name image  button), the SPFieldUserValue will return

–1;#domain\username

rather than

23;#domain\username.
 image

This is a problem when you try to access the SPFieldUserValue.User (SPUser type) property and it returns null.

The Solution
The solution was to get the SPFieldUserValue as a string, and then use the username to then try and resolve for the SPUser object. My rough cut out code is attached – quickly modified for this example. (*Use at your own risk. Is also why I don’t usually post code on my blog!)