Suppose you have a list in SharePoint with a column ‘User’ of type ‘Person or Group’ in it and you are writing custom EventReceivers to add to the list (eg ItemAdded, ItemUpdated, etc). As long as you work on the properties.ListItem and you get the value from properties.ListItem.Item(“User”), you will receive a typical string formatted as <siteusers.id>;#<displayname>. This string can be easily split to separate the ID from the DisplayName. This DisplayName can be passed to SPWeb.EnsureUser() method to retrieve a SPUser object. This object can’t be passed to the UserProfileManager, so sometimes you even have to get the SPUser.LoginName to pass to the UserProfileManager.GetUserProfile() method to get a UserProfile object.

Code example:
Dim web As SPWeb = properties.OpenWeb()
Dim formattedUser As String = properties.ListItem.Item(“User”)
Dim splitUser() As String = formattedUser.Split(“#”)
Dim userDisplayName As String = splitUser(1)
Dim user As SPUser = web.EnsureUser(userDisplayName)

But as soon as you start working with the AfterProperties and BeforeProperties (of type SPItemEventDataCollection) and you get the value of ‘User’, you will notice that you only get the <siteusers.id> (without the DisplayName). You now have two options to continue:

  1. You get the value of ‘display_urn:schemas-microsoft-com:office:office#User’, which contains the DisplayName and continue like in the above example.
  2. Or, you work with the ID and you use the SPWeb.SiteUsers.GetByID() method to retrieve a SPUser object.

Code example:
Dim web As SPWeb = properties.OpenWeb()
Dim beforeProps As SPItemEventDataCollection = properties.BeforeProperties
Dim user As SPUser = web.SiteUsers.GetByID(beforeProps.Item(“User”))

Personally I would suggest to use the second way and rewrite the code above to get the ID out of the formatted string instead of the DisplayName. This way you can write a universal method where you pass the ‘User’ value without bothering about the format of the string you pass. If you can find a ‘;’ you have to split the string on the ‘;’ to retrieve the ID, otherwise you only have the ID in the string.