People or Group field returning semicolon hash then Name
-
07-10-2020 - |
题
In querying the properties of a list item, I found that returning the value of a people or group field that the value would be returned in the form
1;#George Grainger
Using the code:
properties.ListItem["Staff Member"].ToString();
I've worked around the issue, by splitting into 2 strings, but I was wondering if anyone would know of a way to return the value without the ;#
preceding the desired return value?
Note: I realise I could dig through the current web, list and item to query the fieldValue.User.Name
, however this is somewhat resource expensive, and I was hoping for a "cleaner" way.
解决方案
Use below code to get User name:
new SPFieldLookupValue(properties.ListItem["Staff Member"].ToString()).LookupValue;
For User ID:
new SPFieldLookupValue(properties.ListItem["Staff Member"].ToString()).LookupId;
其他提示
My understanding is that by using:
properties.ListItem["Staff Member"].ToString();
You are going to be returned their userID and username, so it will always be returned separated by ;#.
You're probably already using something like:
properties.ListItem["Staff Member"].ToString().split(";#")[1];
To return the username on it's own. You should compare the performance of that against fieldValue.User.Name
and see which is less taxing.
There might be another field value I am missing that you could use in it's place, but I have always just split their ID from the name.