Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

my problem is I have a document library with sub folders. Also the document library got more than one content type.

Is there a way to create a CAML or LINQ query to get all files of a certain content type from one of this sub folders.

The sub folder name is known.

Thanks in advance!

share|improve this question
add comment

2 Answers

up vote 4 down vote accepted

For SPQuery, use SPQuery.Folder to define the folder:

SPFolder folder = list.RootFolder.SubFolders["Folder 1"];

SPQuery query = new SPQuery();
query.Folder = folder;

Then, to define content type, you should include the following condition into your Where clause:

SPContentType contentType = list.ContentTypes["MyContentType"];
query.Query = "<Where><Eq><FieldRef Name='ContentTypeId' /><Value Type='Text'>" + contentType.Id + "</Value></Eq></Where>";

Then, to include all subfolders of "Folder 1", you should specify Scope="Recursive" in the query ViewAttributes:

query.ViewAttributes = "Scope=\"Recursive\"";

P.S. In case you're using Client Object Model and CamlQuery, the syntax will be a bit different. I.e. instead of Folder, you will need FolderServerRelativeUrl:

query.FolderServerRelativeUrl = "/Shared Documents/Folder 1";

And also recursive scope is defined as attribute of the View element, which in this case should be included into query.

share|improve this answer
    
Great answer, but instead of "Name='ContentType'" it must be "Name='ContentTypeId'" when u use contentType.Id. Works perfectly! –  Markus Schmitt Feb 8 '13 at 13:38
    
Oh, my bad, just grabbed this piece from internet and didn't test it :( I'll correct the answer for further readers, thanks! –  Andrey Markeev Feb 8 '13 at 14:08
add comment

You can create CAML query that looks something like this (I've ommited the unrelated parts):

<View>
..........
    <QueryOptions>
        <ViewAttributes Scope='Recursive' />
    </QueryOptions>
    <Query>
      <Where>
          <And>
              <Eq>
                  <FieldRef Name="ContentType"></FieldRef>
                  <Value Type="Text">$Resources:ResourceFileName,ContentTypeName</Value>
              </Eq>
              <Eq>
                  <FieldRef Name="FileDirRef"></FieldRef>
                  <Value Type="Text">Server Relative URL of the Folder</Value>
              </Eq>
          </And>
      </Where>
    </Query>
..........
</View> 

This will return all the documents of the specified content type in the specified subfolder.

Here are some links that might be useful:
Get Items Under Folder CAML and Filter Document library folder through CAML Query

share|improve this answer
    
Can you also show how to specify the folder to query in CAML? –  Jimbo Alba Feb 13 '13 at 4:40
    
I've edited the query. Hope it helps. –  Elitza Atanasova Feb 13 '13 at 11:26
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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