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

Puneeth, C 81 Reputation points

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);

    FieldCollection fColl = tempList.Fields;

    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);               

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

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

      List list = context.Web.Lists.GetByTitle("YOURLISTNAME");  
                        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>   
                        context.Load(items, a => a.IncludeWithDefaultProperties(item => item.File, item => item.File.CheckedOutByUser));  

    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.