By request I'm developing a custom WebPart where it's possible for the user to search files and folders inside a document library, i made a GridView to list the files (working ok I'm not searching), a TextBox for input and a button.

My document library structure is something like this:

  • Document Library
    • Child Folder 1
    • Child Folder 2
      • Grand Child Folder
      • Image.jpg

When using the search I created, I type the name 'Image'. But it always returns nothing (SPListItemCollection.count is always 0).

For the search, i'm using the following code:

SPQuery query = new SPQuery();

query.ViewAttributes = "Scope='RecursiveAll'";

String caml = @"<Where>
                      <FieldRef Name='Title' />
                      <Value Type='Text'>{0}</Value>
                  <FileRef Name='ID' Ascending='True' />

query.Query = String.Format(caml, "Image");
SPListItemCollection resultGetItems = list.GetItems(query);

Anyone can help me with this one? i can't seem to get it working.

Thanks a lot.

  • 2
    Have you set the title of the file? Usually it's the name that is set, so you have to use FileLeafRef as the name in the FieldRef Commented Oct 8, 2012 at 20:09
  • That did it. Thanks a lot!!! I was almost getting there, I saw it was returning a exception on the title right after i posted, but didn't think of the FileLeafRef attribute. Thanks a bunch. Commented Oct 8, 2012 at 20:21

1 Answer 1


Please try modifying the title to "FileLeafRef" in the query.

The internal Column name inside the folder is "FileLeafRef".

Please see the below link:


Please Let me know if it works for you.

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.