10

I have the following JSOM code:

var query = "<View><Query><Where><Eq><FieldRef Name='Category'/><Value Type='Choice'>ValueA</Value></Eq></Where></Query><ViewFields><FieldRef Name='Title'/><FieldRef Name='Value'/><FieldRef Name='Created'/></ViewFields><OrderBy><FieldRef Name='Created' Ascending='FALSE' /></OrderBy><QueryOptions><RowLimit>6</RowLimit></QueryOptions></View>"
camlQuery = new SP.CamlQuery()
camlQuery.set_viewXml(query)
items = list.getItems(camlQuery)
context.load(items)

however the items returned don't seem to be in the order I set ...

1 Answer 1

8

The method of setting the query options is a little different in the Client Object Model. The correct form of your query is:

<View>
    <Query>
        <Where>
            <Eq>
                <FieldRef Name='Category'/>
                <Value Type='Choice'>ValueA</Value>
            </Eq>
        </Where>
        <OrderBy>
            <FieldRef Name='Created' Ascending='FALSE' />
        </OrderBy>
    </Query>
    <ViewFields>
        <FieldRef Name='Title'/>
        <FieldRef Name='Value'/>
        <FieldRef Name='Created'/>
    </ViewFields>
    <RowLimit>6</RowLimit>
</View>

See this for more details: CAML and the Client Object Model.

3
  • Interessting - I used the CAML Designer (karinebosch.wordpress.com/2012/05/12/caml-designer), which suggested my slightly different CAML; which doesn't seem to work - at least not when using the JSOM. Commented Jun 27, 2012 at 11:58
  • Yes the JSCOM uses different format for the CAML. The Caml in my above answer should work with the JSCOM. Commented Jun 27, 2012 at 13:29
  • yes, it does - thx! Commented Jun 28, 2012 at 10:28

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.