Carls Jr.
Carls Jr.

Reputation: 3078

SPQuery sorting issue

I have this SPListItem.Folder in sharepoint that contains a property named "Asset ID".

I have this data in my list

Asset ID  |     Name      |  Asset Type 
    1     |  GamesFolder  |    Games
    2     |  AppsFolder   |    softwares
    3     |  MusicFolder  |    music

In my code I did this

SPList objList = web.Lists["MyList"];
SPQuery query = new SPQuery();

query.Query = "<OrderBy><FieldRef Name='Asset ID' Ascending='FALSE'/></OrderBy>";
query.ViewAttributes = "Scope=\"Recursive\"";               
query.RowLimit = 1;

SPListItemCollection items = objList.GetItems(query);

return objList.Items[0].Folder.Properties["Asset ID"].ToString();

I use .Folder because every entry in the list is a DocumentSet. The returned value is always "1". I don't know what's wrong why my sorting doesn't work at all.

Please help me resolve this issue. Thanks.

Upvotes: 0

Views: 1681

Answers (3)

Anthony Graglia
Anthony Graglia

Reputation: 5435

A little late but if you are having issues, you may be able to use all or a part of the following gist: https://gist.github.com/trgraglia/4672176

And as the accepted answer states, the field name is the issue. You need to use the static name of the field. The static name will always stay the same. Even if the column is renamed. So you should get the column from the column collection by display name and then get the static name from the properties.

Upvotes: 0

Lin Zhang
Lin Zhang

Reputation: 31

or use its internalName:

query.Query = "<OrderBy><FieldRef Name='Asset_x0020_ID' Ascending='FALSE'/></OrderBy>";

Upvotes: 0

V_B
V_B

Reputation: 1569

Hi Carls I think there is issue for field name. U include space in field name

If you want to avoid having to seek out what the internal name of a particular field is, when you first name your column, do not include any spaces or special characters. Once the field (column) has been created, go back and rename the field to include the spaces or special characters as desired. SharePoint will still retain the original field name without spaces and you can use that directly in your query without issue.

Upvotes: 1

Related Questions