0
votes

I'm using a CAML query to grab some items from a list. It pulls the items into a datatable which is then set as the datasource of a gridview control.

Everything worked fine until I realized it was using the ID field to sort the items. I wanted to sort by the field Target_x0020_Id, so I ordered the order by, however it doesn't change the behavior when I added this.

This is my query:

WhereEqFieldRefName='Target_x0020_Id' Value Type='Text'900/Value/EqWhereOrderByFieldRef Name='Target_x0020_Id'/FieldRef/OrderBy

The only thing I added was the orderby element. (Sorry I'm having some issues posting the code without it trying to render in the post)

1

1 Answers

0
votes

I can only assume this is the CAML you're using

<Where>
  <Eq>
    <FieldRefName='Target_x0020_Id'>
    <Value Type='Text'>900</Value>
  </Eq>
<Where>
<OrderBy>
  <FieldRef Name='Target_x0020_Id'></FieldRef>
</OrderBy>

I see three glaring errors.

  1. There's a missing space between FieldRef and Name in the <Eq> portion of the caml.
  2. The closing <where> is missing the forward slash.
  3. The FieldRef tag in <OrderBy> is incorrect. It's self closing

This should be the right caml

<Where>
  <Eq>
    <FieldRef Name='Target_x0020_Id'>
    <Value Type='Text'>900</Value>
  </Eq>
</Where>
<OrderBy>
  <FieldRef Name='Target_x0020_Id' />
</OrderBy>

I do see that you're returning all items where Target_x0020_Id is equal to 900 and then sorting all those items by Target_x0020_Id. Since this field is always 900, your sort isn't going to work.

Also, is Target_x0020_Id really a text field? If it's numeric, you should chagne the type to 'Number' instead of 'Text'.