locked
CAML query not bringing any data RRS feed

  • Question

  • hello, 

    i have this code: 

    SPQuery query = new SPQuery();
              query.Query = "<ViewFields><FieldRef Name='Title' /><FieldRef Name='Created' /></ViewFields><QueryOptions><RowLimit>10</RowLimit></QueryOptions>";
              

    it's a simple query, but I can't get it to work, anybody has any thoughts what this might be?

    Thursday, July 23, 2015 4:49 AM

Answers

  • Hi,

    It should be like this

    SPQuery query = new SPQuery();
    query.ViewFields = "<FieldRef Name='Title' /><FieldRef Name='Created' />";
    query.RowLimit = 10;


    Ram Prasad Meenavalli | MCITP | MCTS SharePoint | MCPD SharePoint | http://www.spdeveloper.co.in

    • Marked as answer by Laura Tam Thursday, July 23, 2015 4:26 PM
    Thursday, July 23, 2015 4:56 AM
  • If you want to order by,  you include it in the query itself, for example:

    SPQuery yourQuery = new SPQuery();
              query.Query = "<OrderBy><FieldRef Name='Created' Ascending='TRUE' /></OrderBy>";
             
    Otherwise, everything has its own property, like the ViewFields or the RowLimit as mentioned by Ram.

    Mohamed Derhalli

    SharePoint Consultant

    Blog: SharePoint Thoughts    Twitter:    LinkedIn:   

    • Marked as answer by Laura Tam Thursday, July 23, 2015 4:26 PM
    Thursday, July 23, 2015 4:10 PM

All replies

  • Hi,

    It should be like this

    SPQuery query = new SPQuery();
    query.ViewFields = "<FieldRef Name='Title' /><FieldRef Name='Created' />";
    query.RowLimit = 10;


    Ram Prasad Meenavalli | MCITP | MCTS SharePoint | MCPD SharePoint | http://www.spdeveloper.co.in

    • Marked as answer by Laura Tam Thursday, July 23, 2015 4:26 PM
    Thursday, July 23, 2015 4:56 AM
  • Thanks Ram, 

    what if I want to order by? do I do it same thing?

    Thursday, July 23, 2015 4:07 PM
  • If you want to order by,  you include it in the query itself, for example:

    SPQuery yourQuery = new SPQuery();
              query.Query = "<OrderBy><FieldRef Name='Created' Ascending='TRUE' /></OrderBy>";
             
    Otherwise, everything has its own property, like the ViewFields or the RowLimit as mentioned by Ram.

    Mohamed Derhalli

    SharePoint Consultant

    Blog: SharePoint Thoughts    Twitter:    LinkedIn:   

    • Marked as answer by Laura Tam Thursday, July 23, 2015 4:26 PM
    Thursday, July 23, 2015 4:10 PM