20

I am trying to retrieve all items that are under a certain folder, in a document library.

This is what i have so far:

List DocumentsList = clientContext.Web.Lists.GetByTitle(list);
CamlQuery camlQuery = new CamlQuery();
camlQuery = new CamlQuery();
camlQuery.ViewXml = @"<View Scope='Recursive'>
                        <Query>
                            <Where>
                                <eq>
                                    <FieldRef Name='FileDirRef'/>
                                    <Value Type='Text'>
                                        /ecm/Business/Business/Projects/IDECO_P01030000
                                    </Value>
                                </eq>
                            </Where>
                        </Query>
                        <RowLimit Paged='TRUE'> 30 </RowLimit>
                    </View>";
ListItemCollection listItems = DocumentsList.GetItems(camlQuery);
clientContext.Load(listItems);
clientContext.ExecuteQuery();

It is definitely a CAML query problem, I am just unsure of how to fix it as yet.

tripleee
  • 132
  • 1
  • 5
Sebastien Stettler
  • 1,270
  • 1
  • 13
  • 28

6 Answers6

33

I managed to get it working.

I changed the scope from "Recursive" to "RecursiveAll", RecursiveAll gets all files and all folders under the specified location.

The CAML query was replaced with

camlQuery.ViewXml = "<View Scope=\"RecursiveAll\"> " +
                "<Query>" +
                "<Where>" +
                            "<Eq>" +
                                "<FieldRef Name=\"FileDirRef\" />" +
                                "<Value Type=\"Text\">/ecm/Business/Business/Projects/IDECO_P01030000</Value>" +
                             "</Eq>" +
                "</Where>" +
                "</Query>" +
                "</View>";
tripleee
  • 132
  • 1
  • 5
Sebastien Stettler
  • 1,270
  • 1
  • 13
  • 28
  • 4
    Sebastien, Your code worked not because you changed the string. It is because you changed the scope from "Recursive" to "RecursiveAll" As RecursiveAll gets all files and all folders under the specidified location. –  Dec 21 '12 at 07:40
  • 1
    hahah, so i see i asked this question when i was till very new to SharePoint :), thanks for pointing that out – Sebastien Stettler Dec 24 '12 at 08:00
  • @SebastienStettler : Hey If i want to check multiple conditions by putting <And> in this query how can i achieve that... – Rahul Gokani May 28 '13 at 04:46
  • 2
    @RahulGokani if you have questions about a specific answer, please use our chat, not comments. Thanx! – Anders Rask Aug 26 '13 at 12:41
  • this is worked ... <View Scope="RecursiveAll"> .. in last two days i want to retrieve all task items in nested folders in task list and search google : summary task nested folders and so on... ( csom clientcontext ) – saber tabatabaee yazdi Feb 17 '14 at 18:54
  • Although this works, it places unnecessary load on SQL subsystem. A more proper way is to use FolderServerRelativeUrl as CMN suggests. – Alexey Krasheninnikov Aug 04 '15 at 14:33
6

Try setting the FolderServerRelativeUrl property of the CamlQuery. See: here

CMN
  • 573
  • 2
  • 7
3

To get items from specific folder without receursive add to query this options:

<QueryOptions>
 <Folder>thelist/thefolder</Folder>
</QueryOptions>

Note to replace thelist/thefolder with actual relative url

gdbdable
  • 210
  • 2
  • 6
3

you should also try to load all the folders and files. means

 clientContext.Load(DocumentList);
 clientContext.Load(DocumentList.RootFolder);
 clientContext.Load(DocumentList.RootFolder.Folders);
 clientContext.Load(DocumentList.RootFolder.Files); 
 clientContext.Load(listItems);
 clientContext.ExecuteQuery();

than you can get FolderCollection in Library

 FolderCollection FC = Document.RootFolder.Folders
 Foreach(Folder Fl in FC)
         {
                FileCollection flc = fl.files;
         } 

Hops its help.

Jignesh Rajput
  • 1,529
  • 7
  • 20
  • 39
2

Have you tried using comparison operator as 'Contains'

    <Contains>
    <FieldRef Name='FileDirRef'/> 
    <Value Type='Text'> 
        /ecm/Business/Business/Projects/IDECO_P01030000 
    </Value> 
    </Contains>
1

This will get you all items in a list or library, including items in folders, support 5000+ items:

private async Task<List<ListItem>> GetAllListItems(ClientContext clientContext, List list)
{
    var result = new List<ListItem>();

    ListItemCollectionPosition itemPosition = null;
    while (true)
    {
        CamlQuery camlQuery = new CamlQuery();
        camlQuery.ListItemCollectionPosition = itemPosition;
        camlQuery.ViewXml = @"<View Scope='RecursiveAll'>
                                <ViewFields>
                                <FieldRef Name='Title'/>
                                </ViewFields>
                                <RowLimit>5000</RowLimit>
                              </View>
                             ";

        ListItemCollection listItems = list.GetItems(camlQuery);
        clientContext.Load(listItems);
        await clientContext.ExecuteQueryAsync();

        itemPosition = listItems.ListItemCollectionPosition;

        foreach (ListItem item in listItems)
        {
            result.Add(item);
        }

        if (itemPosition == null)
        {
            break;
        }
    }

    return result;
}
rlv-dan
  • 342
  • 3
  • 15
  • Woudln't it easier to use ´SP.CamlQuery.createAllItemsQuery()´ or SP.CamlQuery.createAllFoldersQuery()? – Emaborsa Jan 25 '21 at 13:46