locked
Filter Document library folder through CAML Query RRS feed

  • Question

  • Hi Guys,

    I want to fetch all documents from the document library folders and subfolders except only one particular folder.

    I don't want to get any item of this special folder in my query. My folder name is Except .

    I am using below query but it is not working. Any idea, how can i do it.

    string fldname = "Except"; ///Folder Name
    objquery.Query = "<Where><And><Eq><FieldRef Name='Modified'/><Value Type='DateTime'><Today /></Value></Eq><Neq><FieldRef Name='ContentType' /><Value Type='Text'>" + fldname + "</Value></Neq></And></Where>";
    objquery.ViewAttributes = "Scope=\"Recursive\"";
    Cheers, Hemendra-MCTS
    Friday, March 12, 2010 11:28 AM

Answers

  • you can use something like:

    <Neq><FieldRef Name='FileDirRef' /><Value Type='Lookup'>sites/1/mylib/myfolder</Value></Neq>
    make sure that you provide the full server relative url of the folder except the starting slash.
    Monday, March 15, 2010 9:01 AM

All replies

  • Try with this

    <Eq><FieldRef Name='FSObjType'/><Value Type='Lookup'>0</Value></Eq></Where>

    In Query property of SPQuery object, You can set 0 for folder (if item type  = folder) or 1 for List Item/Documents(if ItemType=List Item).



    Ashish Kanoongo, MCP, MCSD, MCTS
    Friday, March 12, 2010 1:24 PM
  • Hi Ashish,

    This query will also give all items from all folders and subfolders. I want to restrict items from one particular folder..

    Can i provide folder name in query so that my query could not fetch any items from this folder.

    please check my query mentioned above
    Cheers, Hemendra-MCTS
    Monday, March 15, 2010 4:30 AM
  • By adding FileDirRef you can limit the view to a particular folder, try this and let me know it works or not

    <Contains>
    <FieldRef Name="FileDirRef" />
    <Value Type="Lookup">Your Folder Name</Value>
    </Contains>

    For more information, see this also, specially Filtering and Ordering
    http://blogs.msdn.com/sharepointdeveloperdocs/archive/2008/01/22/synchronizing-with-windows-sharepoint-services-part-2.aspx

    Keep me updating


    Ashish Kanoongo, MCP, MCSD, MCTS
    Monday, March 15, 2010 4:44 AM
  • Hi Ashish,

    See my query below,
     If i use this query then it will return only one folder items (Folder Name: Except ).

    But my requirement is, return all items from all folders and subfolders except this particular folder...


    string fldname = "Except";// My Folder Name

     objquery.Query = "<Where><And><Eq><FieldRef Name='Modified'/>"
                          + "<Value Type='DateTime'><Today /></Value></Eq>"
                          + "<Contains><FieldRef Name='FileDirRef' /><Value Type='Lookup'>" + fldname  + "</Value>"
                            + "</Contains>"
                            + "</And></Where>";
                        objquery.ViewAttributes = "Scope=\"Recursive\"";


    Cheers, Hemendra-MCTS
    Monday, March 15, 2010 7:06 AM
  • you can use something like:

    <Neq><FieldRef Name='FileDirRef' /><Value Type='Lookup'>sites/1/mylib/myfolder</Value></Neq>
    make sure that you provide the full server relative url of the folder except the starting slash.
    Monday, March 15, 2010 9:01 AM
  • Thanks a ton stefan. problem has been solved

    Cheers, Hemendra-MCTS
    Monday, March 15, 2010 10:20 AM
  • FSObjType for file/item is 0 and for folder it is 1.
    Wednesday, December 17, 2014 11:24 PM