How to ignore both folders and their contents using CAML query in Sharepoint 2019?

Puneeth, C 81 Reputation points
2022-04-08T12:16:26.44+00:00

I am using Sharepoint 2019 and I am trying to fetch only items excluding all the folders and all the items inside those folders.
My current code is given below as is:

List tempList = targetWeb.Lists.GetByTitle(listName);
context.Load(tempList);
context.ExecuteQuery();

    FieldCollection fColl = tempList.Fields;
    context.Load(fColl);
    context.ExecuteQuery();

    CamlQuery camlQuery = new CamlQuery();
    string queryText = "<View><Query><OrderBy><FieldRef Name='ID' Ascending='TRUE'/></OrderBy></Query><RowLimit Paged=\"TRUE\">2</RowLimit></View>";
    camlQuery.ListItemCollectionPosition = position;
    camlQuery.ViewXml = queryText;
    // Executing the query
    ListItemCollection currentCollection = tempList.GetItems(camlQuery);               
    context.Load(currentCollection);
    context.ExecuteQuery();

But this fetching the folders as well into currentCollection. When I used scope=RecusiveAll. it is fetching the items inside the folders and I do not want that either.


Please let me know how to proceed.
Thanks for the help in advance

SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
2,789 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. RaytheonXie_MSFT 33,481 Reputation points Microsoft Vendor
    2022-04-11T06:56:30.8+00:00

    Hi @Puneeth, C ,
    You can try following caml query to get files in library root folder.

      List list = context.Web.Lists.GetByTitle("YOURLISTNAME");  
                        context.Load(list.RootFolder);  
                        context.Load(list.RootFolder.Files);  
                        var items = list.GetItems(  
                            new CamlQuery()  
                            {  
                                ViewXml = @"<View><ViewFields><FieldRef Name='Title' /><FieldRef Name='FileLeafRef' /><FieldRef Name='ID' /><FieldRef Name='ContentType' /></ViewFields><Query>   
                <Where><IsNotNull><FieldRef Name='File_x0020_Type' /></IsNotNull></Where>   
                </Query></View>"  
                            });  
                        context.Load(items, a => a.IncludeWithDefaultProperties(item => item.File, item => item.File.CheckedOutByUser));  
                        context.ExecuteQuery();  
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.