Sunday, June 2, 2013

CAML Queries for finding Document Sets

So you have Document Sets in your library, and you need to list only them, or you are trying to find one in particular.   But when your CAML queries are looking for specific document sets and field values, they turn up nothing.

Well there are two ways around this.  The first involves using a query for a folder object type.

<Eq>
  <FieldRef Name='FSObjType' />
  <Value Type='Number'>1</Value>
</Eq>

This looks for folders specifically, but it also seems to help with Document Sets.  This one is also well documented.

There is another way I found when I was using a tool that wouldn't let me pick FSObjType as a field because it really isn't a field so much as a Content DB... thing.  There is a hidden field called "Level" (internal name "_Level" which you must use in CAML) which appears to indicate the folder level within the library where an item resides.

So if you have a content type derived from Document Set called... ummm... "My Document Sets" for example, and they reside in a library at the root folder, you can find the one you want doing something like this:

<Where>
<And>
  <Something>
     this is where you would search for some other criteria, like a name, title, description, whatever
  </Something>
  <And>
    <Eq>
      <FieldRef Name='_Level' />
      <Value Type='Number'>1</Value>
    </Eq>
    <Eq>
      <FieldRef Name='ContentType' />
      <Value Type='Text'>My Document Sets</Value>
    </Eq>
  </And>
</And>
</Where>

With the Level field specified it seems to force SharePoint to include folders at the level specified.  This will get you around using some other tool or product that won't let you specify FSObjType, but if that tool or product can find hidden fields, Level should be in there.

Good luck.

No comments:

Post a Comment