Share via


CAML query to get files from Doc lib with specific file extension

Question

Wednesday, August 24, 2011 2:11 PM | 1 vote

i want to retrive Jpeg/txt files from Doc lib, how i can achive that using CAML or by any other option/API.

After searching on net i came to know there will be "FileExtension" column available but I tried my self with

"<Where><Eq><FieldRef Name='FileExtension'><Value Type='text'>txt</Value></FieldRef></Eq></Where>" query ,

it throws exception saying field doesn't exists.

All replies (4)

Wednesday, August 24, 2011 3:30 PM âś…Answered | 2 votes

Note sure this is the best way to achieve this but you should be able to use:

<Where><Or><Contains><FieldRef Name='FileLeafRef' /><Value Type='Text'>.jpg</Value></Contains><Contains><FieldRef Name='FileLeafRef' /><Value Type='Text'>.txt</Value></Contains></Or></Where>

But you will need to add a FieldRef to the FileLeafRef in your ViewFields property of the SPQuery object.

Or just try adding:

query.ViewFields = "<FieldRef Name='FileExtension' /><FieldRef Name='Title' />";

to your SPQuery object, and make sure you add all the fields you need to reference as fieldrefs in the ViewFields property. 


Wednesday, August 24, 2011 3:57 PM | 1 vote

Actually, you can open the view in SPD and filter on File Type. The CAML will look like this:
                 <Where>
                        <Eq>
                            <FieldRef Name="File_x0020_Type"/>
                            <Value Type="Text">pdf</Value>
                        </Eq>
                    </Where>
I used pdf in this example.
AK


Thursday, August 25, 2011 7:07 AM

Thanks for the reply .

Just using Contains ,,i could achieve my req, no need to add any viewfields to it.


Thursday, June 28, 2012 5:33 PM

Not clear what you mean here by 'just using Contains'?

I think this is exactly what I want to do...

When I look at the xml I'm getting  back from my usual getListItems (off the lists webservice).. each row has a "ows_File_q0020_Type" that looks like it's the filetype.

I'm working in vb.net.donat have a SPQuery object.. I don't think..?