SharePoint 2013 CAML query OrderBy not sorting properly

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)


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.

  • There's a missing space between FieldRef and Name in the <Eq> portion of the caml.
  • The closing <where> is missing the forward slash.
  • 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'.

    链接地址: http://www.djcxy.com/p/64150.html

    上一篇: 如何以编程方式读取MetaInfo字段值?

    下一篇: SharePoint 2013 CAML查询OrderBy未正确排序