1

I'm attempting to retrieve items from a subfolder in a document library. I must get these items where value is 'Like'. How can I do this via C# using the SP object model or by CAML query?

this is what I have so far:

        using (SPSite site = new SPSite("http://mySite/Shared Documents/"))
        {
            using (SPWeb web = site.OpenWeb())
            {
                //get sub folder url
                string SubFolderUrl = web.ServerRelativeUrl + "/shared documents/SubFolderName";

                //able to get the subfolder at this point
                SPFolder SubFolder = web.GetFolder(SubFolderUrl);

                //Now what could I do from here to get the items in the subfolder?
                //use SPQuery object against the folder?
            }
         }

any suggestions are welcomed, thanks.

2
  • When you say "where value is 'Like'," do you mean that you want to get files with names similar to a given string, or do you just want to get a list item with a specific name? Commented Jul 11, 2014 at 18:25
  • sorry for the ambiguity. When I say 'Like' I mean it in the same terms as a sql query 'Like'. ie Select * from tbl where attribute Like <value>
    – KidBatman
    Commented Jul 11, 2014 at 18:30

1 Answer 1

3

We can indeed use SPQuery for this. Here's some code that does what you want to do:

using (SPSite site = new SPSite("http://mySite/Shared Documents/"))
{
    using (SPWeb web = site.OpenWeb())
    {
        //get sub folder url
        string SubFolderUrl = web.ServerRelativeUrl + "/shared documents/SubFolderName";

        //able to get the subfolder at this point
        SPFolder SubFolder = web.GetFolder(SubFolderUrl);

        //create an SPQuery that gets us what we want
        string fileName = "FileName";
        SPQuery query = new SPQuery();
        query.Query = "<Where><Contains><FieldRef Name=\"Title\" /><Value Type=\"Text\">" + fileName + "</Value></Contains></Where>";
        query.Folder = SubFolder;  // This should restrict the query to the subfolder

        //run the SPQuery against the SPList
        SPDocumentLibrary list = SubFolder.DocumentLibrary;
        SPListItemCollection files = list.GetItems(query);
    }
}

This code takes whatever the search query should be in the string fileName and returns all files with fileName in its Title.

The SPListItemCollection files should contain the files you're looking for. Please note that I haven't tested this code nor have I done error checking (you should probably check if the SPDocumentLibrary list is not null at least).

9
  • thanks for the reply. I get a bevy of errors when trying to save to SPListItemCollection object. I'm currently trying to see if I can save to SPFileCollection instead, but have not had any luck so far.
    – KidBatman
    Commented Jul 11, 2014 at 20:11
  • Well, what's the first error message? Is list not null? If it isn't, maybe something's wrong with the CAML query -- if this is the case, see what happens when query.Query is not set. This should return all items in the folder. Commented Jul 11, 2014 at 20:38
  • list is not null. I am able to step over SPListItemCollection files = ... without any hitch. When I rexamined the object however, all properties in files are throwing exceptions.
    – KidBatman
    Commented Jul 11, 2014 at 20:51
  • Please excuse the "Is your computer plugged in" question, but you changed the string fileName = "FileName"; into something that matches a SPListItem title in that subfolder, right? Commented Jul 11, 2014 at 20:54
  • lol well I opted to put the value directly in the query and save myself the typing of string fileName.... But to answer your question, yes I did reference the title of an item in the subfolder.
    – KidBatman
    Commented Jul 11, 2014 at 21:06

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.