Share via


CAML query to get specific file from folder

Question

Tuesday, May 19, 2020 9:17 PM

Hello , I need to get a specific file from a folder in sharepoint. Im using CAML query to get the files but not getting close to get a specific.

Im doing the following :

                                                                          

<View Scope=\"RecursiveAll\"> " +
                    "<Query>" +
                    "<Where>" +
                                "<Eq>" +
                                "<Contains>" +
                                    "< FieldRef Name = \"Title\" />" +
                                    "< Value Type = \"Text\" > 1_0000000227</ Value >" +
                                    "</Contains>" +
                                    "<FieldRef Name=\"FileDirRef\" />" +
                                    "<Value Type=\"Text\">/sites/GEDBand/Documentos/SALUNO/1_0000000227</Value>" +
                                    
                                            "</Eq>" +
                                 
                    "</Where>" +
                    "</Query>" +
                    "</View>";

                                 Thanks

Rodrigo S. Nascimento

All replies (2)

Tuesday, May 19, 2020 10:10 PM ✅Answered | 1 vote

I don't think you can get the specific file using CAML Query but can check for the file if it exists using below query. And also from the below article you can find couple of other example check below.

CamlQuery camlQuery = new CamlQuery();
    camlQuery.ViewXml = @"<View Scope='Recursive'>
                        <Query>
                            <Where>
                                <Eq>
                                    <FieldRef Name='FileLeafRef'></FieldRef>
                                    <Value Type='Text'>" + fileName + @"</Value>
                                </Eq>
                            </Where>
                        </Query>
                </View>";
    camlQuery.FolderServerRelativeUrl = folderServerRelativeUrl;
    ListItemCollection listItems = DocumentsList.GetItems(camlQuery);
    clientContext.Load(listItems);
    clientContext.ExecuteQuery();
 
    return listItems.Count > 0;
}
U

https://pholpar.wordpress.com/2018/03/23/how-to-check-if-a-specific-file-exists-in-a-folder-structure-of-a-sharepoint-document-library-using-the-client-object-model/

https://www.sharepointdiary.com/2017/11/sharepoint-online-how-to-use-caml-query-in-powershell.html

Thanks & Regards,

sharath aluri


Wednesday, May 20, 2020 5:31 AM ✅Answered | 1 vote

Hi Rodrigo,

For filtering file item Title field value in a specific sub folder, here is the code snippet for your reference:

                List list = ctx.Web.Lists.GetByTitle("Documents");
                CamlQuery caml = new CamlQuery();
                caml.ViewXml= "<View Scope='Recursive'><Contains><Where><Eq><FieldRef Name='Title'/><Value Type='Text'>1_0000000227</Value></Contains></Where></Query></View>";
                caml.FolderServerRelativeUrl = "/shared%20documents/ArchDocs";
                ListItemCollection filteredItems = list.GetItems(caml);
                ctx.Load(filteredItems);
                ctx.ExecuteQuery();

caml.FolderServerRelativeUrl is used to set sub folder relative url so that the query will against specific sub folder.

Thanks

Best Regards

Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact [email protected].

SharePoint Server 2019 has been released, you can click

here to download it.
Click

here to learn new features. Visit the dedicated

forum to share, explore and talk to experts about SharePoint Server 2019.