locked
SPQuery on "Pages" document library RRS feed

  • Question

  • Hello Guys,

    I created a custom content type on my SP2010 intranet and i wish to retrieve the last '4' items sorted by descenting date contained in the "Pages" Document library.

    This my code

    SPQuery query = new SPQuery();
    query.Query = "<Query><Where><Eq><FieldRef Name='ContentType' /><Value Type='Computed'>News</Value></Eq></Where><OrderBy><FieldRef Name='Created' Ascending='true' /></OrderBy></Query>";
    query.RowLimit = 4;
    SPListItemCollection items = library.GetItems(query);

    but, for a reason that I am not able to undestand, the query does not filter based on the content type and, moreover, the items are not ordered by descending order.

    Any suggestion?

    Thank you very much,
    Annio :)

    Tuesday, February 28, 2012 11:29 AM

Answers

  • Hello Annio,

    Remove the opening and closing <Query> tags in the query property. It should be like

    query.Query = "<Where><Eq><FieldRef Name='ContentType' /><Value Type='Computed'>News</Value></Eq></Where><OrderBy><FieldRef Name='Created' Ascending='true' /></OrderBy>";

    This will filter the items as expected


    Ram Prasad Meenavalli | MCTS SharePoint 2010 | MCPD SharePoint 2010

    Tuesday, February 28, 2012 11:43 AM
  • This query is correct and should work.

    Use the Type as Computed only, and make sure you are giving the correct name of the content type in the query.

    I tested this and its working for me.


    Ram Prasad Meenavalli | MCTS SharePoint 2010 | MCPD SharePoint 2010

    Tuesday, February 28, 2012 4:35 PM

All replies

  • Hello Annio,

    Remove the opening and closing <Query> tags in the query property. It should be like

    query.Query = "<Where><Eq><FieldRef Name='ContentType' /><Value Type='Computed'>News</Value></Eq></Where><OrderBy><FieldRef Name='Created' Ascending='true' /></OrderBy>";

    This will filter the items as expected


    Ram Prasad Meenavalli | MCTS SharePoint 2010 | MCPD SharePoint 2010

    Tuesday, February 28, 2012 11:43 AM
  • Thanks Ram, the ordering is working now but if I keep the filtering the query does not return any element now.

    This is the "new" query:

    query.Query = "<Where><Eq><FieldRef Name='ContentType' /><Value Type='Computed'>News</Value></Eq></Where><OrderBy><FieldRef Name='Created' Ascending='true' /></OrderBy>";
    

    I tried changing from "computed" to "text" with no effects.

    I tried filtering for the default content types (Pages) using all the other clauses (begins with, contains) but still no result-set

    I obtained a result-set only searching using the "IsNotNull" clause. That basically means that the query is able to find the "Content type" fieldref.

    Thanks for the help :)

    Tuesday, February 28, 2012 12:26 PM
  • This query is correct and should work.

    Use the Type as Computed only, and make sure you are giving the correct name of the content type in the query.

    I tested this and its working for me.


    Ram Prasad Meenavalli | MCTS SharePoint 2010 | MCPD SharePoint 2010

    Tuesday, February 28, 2012 4:35 PM