We have a document library on our Intranet where documents are stored. The document library also has 2 folders with broken inheritance. I want to loop through all the documents in the document library, including documents in the folders.

This is my code where I try to retrieve all documents with a given content type.

private void IterateItems(SPList list, string contentTypeId)
    var query = new SPQuery { Query = GetItemsByContentType(contentTypeId) };   
    var itemCollection = list.GetItems(query);


private static string GetItemsByContentType(string contentTypeId)
    return  "</Where>" +
            "<BeginsWith>" +
                "<FieldRef Name='contentTypeId' />" +
                "<Value Type='contentTypeId'>" + contentTypeId + "</Value>" +
                "</BeginsWith>" +
                "</Where>" +
                "<QueryOptions>" +
                "<ViewAttributes Scope='RecursiveAll' />" +

The problem is that when this code is executed it does not return the documents that are inside folders. It only returns documents at the top level in the document library. I have executed the CAML query inside a tool, CAML designer, and that returned all documents including those in the folders.

So, now I am wondering why the same query is not returning the desired result when run with server object model.

I run this code as an administrator, so permissions should not be the issue. Any tips or ideas?

1 Answer 1



 query.ViewAttributes = "Scope=\"Recursive\"";
  • Thanks man. Saved my day. Used my whole day to fix this.
    – Ilyas
    May 28, 2015 at 15:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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