0

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>
                    <Contains>
                      <FieldRef Name='Title' />
                      <Value Type='Text'>{0}</Value>
                    </Contains>
                </Where>
                <OrderBy>
                  <FileRef Name='ID' Ascending='True' />
                </OrderBy>";

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 – Per Jakobsen Oct 8 '12 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. – C. Hoffmann Oct 8 '12 at 20:21
2

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

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

Please see the below link:

http://sudarsansharepointblog.blogspot.in/2012/10/caml-query-documents-from-sharepoint.html

Please Let me know if it works for you.

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.