When I’m evaluating a SharePoint list’s custom field of type ‘User’ I’d expect to only get the username but instead I’m seeing additional characters at the start that I need to trim out.
SPContext.Current.ListItem[NameOfUserField] has the value of
“14;#DeeMac”
And as a side note – this field is being treated as a string – but in my xml files I’ve defined it as a ‘User’ field type – where and why has this conversion been made? I did originally try to cast it as SPFieldUser which failed.
The first part of the string is that user’s ID in the
SPWebin question. I.e. DeeMac is, roughly speaking, the 14th person to use that web.In order to get an
SPUserobject from thatstring, you need to take theSPWebto which the user belongs and call theSPFieldUserValue(SPWeb, string)constructor with the two.The
SPFieldUserValuethus constructed contains aUserproperty of typeSPUser, which may be null if the string was null/empty.Note that sometimes, I think, a user column may actually contain
SPFieldUserValueobjects already, instead ofstrings.Also if it’s a multi-user column, it might contain a
SPFieldUserValueCollectionobject or astringto pass into that class’s constructor.If you ever run into issues, debug and check what’s actually in your column.