none
Get files in a sub folder using CamlQuery client object RRS feed

  • Question

  • Hi

    i need to retreive files located in sub folders of a document library. My input parameter will be sub folder name. Can you please suggest how to acheive this. i tried following query , but no luck

     camlQueryDoc.ViewXml =
                                                    "<View Scope=\"RecursiveAll\">" +
                                                    "<Query>" +
                                                     "<Where>" +
                                        "<Eq>" +
                                        "<FieldRef Name='FileLeafRef'/>" +
                                        "<Value Type='Text'>"+ApplicationNo+"</Value>" +
                                        "</Eq>" +
                                        "</Where>" +
                                                    "</Query>" +
                                                    "<RowLimit>5</RowLimit>" +
                                                    "</View>";

     SEDocument = extList.GetItems(camlQueryDoc);
                                                SPcontext.Load(SEDocument);
                                                SPcontext.ExecuteQuery();

    Thanks in advance

    Regards

    Vinay

    Wednesday, February 1, 2017 12:23 PM

Answers

  • Hi Vinay,

    We can set the sub folder url in FolderServerRelativeUrl  property for CamlQuery object like below to get files in sub folder:

    public static void GetListItemsInFolder()
    {
                ClientContext clientContext = new ClientContext("http://basesmc2008");
                List list = clientContext.Web.Lists.GetByTitle("tester2");
                CamlQuery camlQuery = new CamlQuery();
                camlQuery.ViewXml = @"<View Scope='Recursive'>
                                        <Query>
                                        </Query>
                                    </View>";
                camlQuery.FolderServerRelativeUrl = "/tester2/whatdown";
                ListItemCollection listItems = list.GetItems(camlQuery);
                clientContext.Load(listItems);
                clientContext.ExecuteQuery();
                ListItem itemOfInterest = listItems[0];
                string creator = itemOfInterest.FieldValues["Created_x0020_By"].ToString();
                string title = itemOfInterest.FieldValues["Title"].ToString();
            }

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Thursday, February 2, 2017 3:18 AM

All replies

  • Hi Vinay,

    We can set the sub folder url in FolderServerRelativeUrl  property for CamlQuery object like below to get files in sub folder:

    public static void GetListItemsInFolder()
    {
                ClientContext clientContext = new ClientContext("http://basesmc2008");
                List list = clientContext.Web.Lists.GetByTitle("tester2");
                CamlQuery camlQuery = new CamlQuery();
                camlQuery.ViewXml = @"<View Scope='Recursive'>
                                        <Query>
                                        </Query>
                                    </View>";
                camlQuery.FolderServerRelativeUrl = "/tester2/whatdown";
                ListItemCollection listItems = list.GetItems(camlQuery);
                clientContext.Load(listItems);
                clientContext.ExecuteQuery();
                ListItem itemOfInterest = listItems[0];
                string creator = itemOfInterest.FieldValues["Created_x0020_By"].ToString();
                string title = itemOfInterest.FieldValues["Title"].ToString();
            }

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Thursday, February 2, 2017 3:18 AM
  • Thanks for the reply Jerry

    in the above example, i need to know exact location of the sub folder which i dont know 

    camlQuery.FolderServerRelativeUrl = "/tester2/whatdown";

    Insteadm i get dynamically the value of "whatdown". It can be in sub folders also.

    for e.g.. the location can be /tester2/whatdown or /tester2/testsub1/whatdown

    Please suggest me on this

    regards,
    Vinay

    Friday, February 3, 2017 3:07 AM
  • Hi Vinay,

    I understand you are using CAML query to get the files from sub folders in a Document Library but it is very easier with REST API calls in SP 2013.

    Have a look at the below code:

    var folderUrl = '/documents/2017';
    var url = _spPageContextInfo.webServerRelativeUrl + "/_api/Web/GetFolderByServerRelativeUrl('" + folderUrl + "')?$expand=Folders,Files";
    
    $.getJSON(url,function(data,status,xhr){
    
        for(var i = 0; i < data.Files.length;i++){
            console.log(data.Files[i].Name);    
        }
    
        for(var i = 0; i < data.Folders.length;i++){
            console.log(data.Folders[i].Name);    
        }
    });
    /_api/web/Lists/GetByTitle('DocLib')/GetItems(query=@v1)?$select=Title,File/Name&$expand=File&@v1={"ViewXml":"<View Scope='RecursiveAll'><Query><Where><Eq><FieldRef Name='FileDirRef' /><Value Type='Text'>/SubSite1/SubSite1.2/DocLib/SubFolder1/SubFolder1.3</Value></Eq></Where></Query></View>"}
    

    Hope this helps you!


    Cheers,
    Maruthu
    Please click the 'Mark as Answer' if this post solves your problem or "Vote As Helpful" if it was useful! :)
    Explore the SharePoint

    Friday, February 3, 2017 4:27 AM
  • Thank you marathu for your suggestion on using REST API

    regards,
    Vinay

    Wednesday, February 8, 2017 10:06 AM