1

I have a folder (Myfolder) that includes two subfolder2.

   Myfolder
    -----Subfolder1
    --------file1.zip
    --------file2.zip
    -----Subfolder2

Inside the subfolders there are zip files. I cannot find them using a CamlQuery

string res = "";

List list = context.Web.Lists.GetByTitle("Myfolder");
CamlQuery query = new CamlQuery();
query.ViewXml = @"<View Scope='RecursiveAll'><Query><Where>" + 
                    "" +
                    "<Contains><FieldRef Name='FileLeafRef' /><Value Type='Text'>.zip</Value></Contains>" + 
                    "</Where></Query></View>";

ListItemCollection listitems = list.GetItems(query);
context.Load(listitems);
context.ExecuteQuery();

foreach (ListItem listItem in listitems)
{
    res += "" + listItem["Title"];
}

return res; // res is empty

Is there anything wrong?

1

You should use 'ExecuteNonQuery`

var siteUrl = "/";
var res = "";
var clientContext = new SP.ClientContext(siteUrl);
var oList = clientContext.get_web().get_lists().getByTitle('MyFolder');    
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml("<View Scope='RecursiveAll'><Query><Where>" + 
                "" +
                "<Contains><FieldRef Name='FileLeafRef' /><Value Type='Text'>.zip</Value></Contains>" + 
                "</Where></Query></View>");
this.collListItem = oList.getItems(camlQuery);
clientContext.load(collListItem);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));        

function onQuerySucceeded(sender, args) {
    var listItemEnumerator = collListItem.getEnumerator();
    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();  
        res += " " + oListItem.get_item("FileLeafRef");
    }    
}

function onQueryFailed(sender, args) {
    //Handle Error
}
| improve this answer | |
1

Use CamlQuery.FolderServerRelativeUrl property for specify the server relative URL of a list folder from which results will be returned.

Example

Assume we need to retrieve zip files from Folder Archive located in Documents library

var fileType = "zip";
var list = ctx.Web.Lists.GetByTitle("Documents");
var query = new CamlQuery();
query.FolderServerRelativeUrl = "/Documents/Archive";
query.ViewXml = string.Format(@"<View Scope='RecursiveAll'><Query><Where><And><Eq><FieldRef Name='FSObjType' /><Value Type='Integer'>0</Value></Eq><Eq><FieldRef Name='File_x0020_Type' /><Value Type='Text'>{0}</Value></Eq></And></Where></Query></View>",fileType);
var items = list.GetItems(query);
ctx.Load(items);
ctx.ExecuteQuery();

foreach (var item in items)
{
    Console.WriteLine(item["FileRef"]);
}

Note: File_x0020_Type field is used in query to filter files by type

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.