2
votes

Using Sharepoint 2007 and trying to filter a list of items by a field called StudentName:

<Field
ID="{GUID-REDACTED}"
Name="StudentName"
DisplayName="Student Name"
Type="User"
Group="STUDENT COLS" />

Currently, this is the CAML used to filter, when its run through U2U CAML Query Builder, it returns the correct list items without a problem, however when its deployed to SharePoint, it returns the entire list (i.e. no filtering).

SPQuery userQuery = new SPQuery();
userQuery.Query = "<OrderBy>
<FieldRef Name='Rank'>
</FieldRef>
</OrderBy>
<Where>
    <Eq>
        <FieldRef Name='StudentName' LookupId='TRUE' />
        <Value Type='Integer'><UserID /></Value>
    </Eq>
</Where>"

SPListItemCollection userProjectBasket = PBL.GetItems(userQuery);

I've tried it with and without Query tags, to no avail and i've also changed the type of the userID to User as a last resort, still no joy.

Very stumped, so any input warmly welcomed. Thanks.

2
Have you recycled the app pool so that the deployed component is used?Chriseyre2000
I think so, just recycled in case it was missed, still having the same issuesJ Thorpe

2 Answers

2
votes

The OrderBy caluse needs to come after the Where clause:

userQuery.Query = 
@"<Where>
    <Eq>
        <FieldRef Name='StudentName' LookupId='TRUE' />
        <Value Type='Integer'><UserID /></Value>
    </Eq>
</Where>
<OrderBy>
    <FieldRef Name='Rank'/>
</OrderBy>";
-2
votes

Good answer - but note one thing - when using the SPQuery object, you NEVER specify the tags (these are internally included in the SPQuery.Query object).

Also, you can still use the U2U CAML creator to save you the time of building the query itself (http://www.u2u.be/Tools/SharePointCamlQueryBuilder.aspx); though it was built for 2007, it works for 2010 too.

David Sterling/SICG - http://www.sterling-consulting.com