So recently I had to perform some custom code that would add an entry from a people picker control to a user\group column and back again (on another form).  Going from the people picker control to SharePoint is really easy (the code snippet’s below are obviously  I am trying to keep most of my examples on this site in C#, but this particular client I wrote this for was a VB shop at the time).

Protected Sub btnSave_Onclick(sender As Object, e As System.EventArgs) Handles btnSave.Click
'I use a dictionary here as I find it a very efficient method for holding all
'my data columns and their values (I have a lot more than shown here)
Dim dataToInsert As Dictionary(Of String, String) = New Dictionary(Of String, String)
Dim spControl As SharePointControl = New SharePointControl 'SharePointControl is a custom class for all my SP data access

If (ppManager.ResolvedEntities.Count > 0) Then
Dim manager As PickerEntity = TryCast(ppManager.ResolvedEntities(0), PickerEntity)
dataToInsert.Add(AttendanceSupportStrings.ManagerColumn, manager.Key)
lblMessage.Text = "Manager field cannot be blank"
Exit Sub
End If
'do other validation and value storage here
spControl.AddListItem(SPContext.Current.Site, dataToInsert)
Catch ex as exception
lblMessage.Text = (String.Format("An error occurred adding this entry.  Error returned: {0}", ex.Message))
lblMessage.Visible = True
End Try


Next, cast the value to an SPUser type and update the list. Please note: I realize some of you may be wondering why I am not going directly from the people picker to SPUser.  The reason being is that in most cases you are updating more than one field at a time and I wanted to show you a method (at least the one I use) to store columns of multiple types

'Use the dictionary key as the column name and the value as the column value
Sub AddListItem(sPSite As SPSite, dataToInsert As Dictionary(Of String, String))

Using Web As SPWeb = sPSite.OpenWeb
Dim splist As SPList = Web.Lists(AttendanceSupportStrings.AttendanceListName)
Dim listItem As SPListItem = splist.AddItem()

For Each dictEntry As KeyValuePair(Of String, String) In dataToInsert
Select Case (dictEntry.Key)
Case AttendanceSupportStrings.ManagerColumn
listItem(dictEntry.Key) = Me.GetEmployeeByID(sPSite, dictEntry.Value)
'More conditional updates here
End Select
End Using

Private Function GetEmployeeByID(sPSite As SPSite, userName As String) As SPUser

Dim serviceContext As SPServiceContext
serviceContext = SPServiceContext.GetContext(sPSite)
Dim profileManager As New UserProfileManager(serviceContext)

If (profileManager.UserExists(userName)) Then
Dim spUser As SPUser = sPSite.OpenWeb.EnsureUser(userName)
Return spUser
Throw New UserNotFoundException("This user is not found in SharePoint.  For the workflow process to proceed ensure user has valid email")
End If

End Function


That may seem like a lot of code just to upload, but remember it was written to handle more than just the one column. It also illustrates a method to upload an entire list row’s worth of data.

Now to go from a User Column to a people picker is quite a bit less code because I am only doing it one column at a time and access the column directly.  I am not building a dictionary to hold different field types nor updating a number of fields at once.  However, I do think that going from a User column to a People Picker is a bit more convoluted.  You actually first cast the user column into a SPUser object, then you put the user object’s login name into a Picker Entity.  You then add the picker entity to an ArrayList and finally you then add the array list to the picker object.

If (Not spListItem(AttendanceSupportStrings.UnionRepColumn) Is Nothing) Then
Dim unionContact As PickerEntity = New PickerEntity()
Dim unionList As ArrayList = New ArrayList()
Dim userUnion As SPUser = New SPFieldUserValue(spListItem.Web, spListItem(AttendanceSupportStrings.UnionRepColumn).ToString).User

unionContact.Key = userUnion.LoginName

End If


So hopefully this illustrates a method that can be used to take a value from a People Picker in a custom form up to SharePoint and then back again.