What I want to achieve is: get a bunch of SPListItems from an SPList, using SPQuery, and have the items ordered by an SPFieldUser column. Just referencing the SPFieldUser column appears to sort on the LoginName. My question is: is it possible to have it sorted on e.g. SPUser.Name, i.e. the real name of the user?

EDIT: Generally speaking, when sorting on e.g. a number field or a string field there are no surprises on the outcome, but what happens when you want to sort on a complex field like SPUser? SPUser.LoginName and SPUser.Name would result in different orderings in most cases.

2 Answers 2


The sorting of Lookup columns (and a SPFieldUser is just a lookup column into the User Information List) depends on the LookupField of the lookup column.

If you define your User field to display as one of the Name columns then the sorting of your Query should by the display name of the user.

  • Thanks! I wasn't aware that SPFieldUser was a lookup column. I'll look into it!
    – claesv
    Commented Sep 26, 2012 at 12:27

ok, im presuming that you are creating the list using xml and not querying in .cs?

whats the full code and are you doing it like this?

    <FieldRef  Name="SomeUserField"/>

You dont need to add in ascending = true as by default it is ascending. You only need to use it if you want to go in decending order by setting it to false!

and that should be encapsulated in the view like so:

<View BaseViewID="0" Type="HTML" MobileView="TRUE" TabularView="FALSE"> 
<Toolbar Type="Standard" /> 
<XslLink Default="TRUE">main.xsl</XslLink> 
    <FieldRef Name="Title"/>
    <FieldRef Name="SomeUserField"/>
    <FieldRef  Name="SomeUserField"/>


for orderby query reference to this for help :)



full xml list (Query Schema)


hope this helps :)


ok that would make sense :) , have you tried the following? tbh without seeing your code I wouldnt know where your going wrong!

SPQuery spQuery = new SPQuery();
spQuery.Query = "<OrderBy><FieldRef Name='SomeUserField' /></OrderBy>";

SPList list = web.GetList(listUrl);
SPListItemCollection items = list.GetItems(spQuery);

You can find more indepth here:



If you want to replace the SPFieldUser to SPUser.name for the real name you need to cast or convert the object and loop through each one.

SPFieldUser userField = (SPFieldUser)item.Fields.GetField("LoginName");

SPFieldUserValue userValue = (SPFieldUserValue)userField.GetFieldValue(item["LoginName"].ToString());

SPUser user = userValue.User;

As an example the above code gets the spuser from the SPFieldUser. "LoginName" is the colum name your refering to change. Sorting on any list is possible, in the end its text your sorting on and its in a list!

for more of an understanding you should checkout this:




I hope its what your looking for or again iv missunderstood you lol.

hope it helps :)

  • Actually no, I'm doing it from code using SPQuery. Please note that the query "sort of" works, as it seems to sort on some kind of user ID (a number) whereas I want to sort on the name or username, i.e. SPUser.Name or SPUser.LoginName.
    – claesv
    Commented Sep 24, 2012 at 8:47
  • ok iv ammended the answer
    – Ali Jafer
    Commented Sep 24, 2012 at 9:33
  • Ok, actually it does seem to work in that it sorts on LoginName, e.g. Domain/UserA, Domain/UserB etc, but I would like to sort on the actual name of the user, but I guess that can't be done. I'll update my question.
    – claesv
    Commented Sep 24, 2012 at 11:51
  • ok so what are you using to populate the names?
    – Ali Jafer
    Commented Sep 24, 2012 at 12:28
  • iv just read through your question, to understand it properly you want to sort on real name but have the login name displayed?
    – Ali Jafer
    Commented Sep 24, 2012 at 12:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.