Share via


CAML Query against content type

Question

Wednesday, December 14, 2011 12:09 AM

Hi

I am trying to query a Document library by content type using CAML:

SPQuery qry = new SPQuery();

string query = <Where><BeginsWith><FieldRef Name='ContentTypeId'/><Value Type='Text'>0x0101003de59dc6676b4ab49e4eb231a59ac906</Value></BeginsWith></Where>;

qry.Query = query;

SPListItemCollection items = docLib.GetItems(qry);
int itemCount = items.Count(); //this is 0

When I run this, no items are returned.  However, when I go into the default view of the document library and filter against the content type, I can see documents are associated to that content type.  Was wondering why I can see the documents in the filtered default view, but the CAML query does not return any items. 

Any advice or direction to some online resource would be greatly appreciated.

Thank you in advance

Rachael

All replies (9)

Wednesday, December 14, 2011 2:03 AM ✅Answered

Your query is valid. The only thing possibly is that the contenttypeid value you are using is incorrect.Blog | SharePoint Field Notes Dev Tool | ClassMaster


Friday, December 16, 2011 4:25 AM ✅Answered

I tested on a document library with document content type, the following two works:

<Where><BeginsWith><FieldRef Name='ContentTypeId'/><Value Type='Text'>0x0101007764851CB5A6344EB5C9E35447D01F39</Value>

 

<Where><BeginsWith><FieldRef Name='ContentTypeId'/><Value Type='Text'>0x0101</Value>

 

0x0101007764851CB5A6344EB5C9E35447D01F39 is the content type id of the list content type and 0x0101 is the content type id of the site content type.

but what follows does not work:

<Where><BeginsWith><FieldRef Name='ContentTypeId'/><Value Type='Text'>0x01010</Value>

By the way, i sugguest using the value type ContentTypeId instead of Text as the CAML generated by LINQ described in: 

http://msdn.microsoft.com/en-us/library/ff798478.aspx


Wednesday, December 14, 2011 2:21 AM

What Steve said - it may be that you are using the fields display name instead of it's internal name. You can review how to obtain the internal name here: http://allaboutmoss.com/2010/05/11/3-ways-to-find-sharepoint-list-fields-internal-name/Keith Tuomi | Twitter: @itgroove_keith | Blog: http://yalla.itgroove.net

Please click "Propose As Answer" if a post solves your problem or "Vote As Helpful" if a post has been useful to you.


Monday, December 22, 2014 10:13 AM | 1 vote

Instead of 'Text' use 'ContentTypeId' in the Value Type attribute, like so:

string query = <Where><BeginsWith><FieldRef Name='ContentTypeId'/><Value Type='ContentTypeId'>0x0101003de59dc6676b4ab49e4eb231a59ac906</Value></BeginsWith></Where>;

It worked for me, It has to do with an index on Contenttype which is used and uses a special value type.


Friday, October 23, 2015 8:11 AM

Same for me here,<Value Type='Text'> works on 5 sites but not on 2 others with same solution deployed..

<Value Type='ContentTypeId'> works everywhere !


Tuesday, July 11, 2017 9:15 AM

I reach this post today because i find that the script from https://dotnetbattlefield.taurit.pl/sharepoint-get-folders-spquery/   that works on my DEV system does not work on client PROD system.

my DEV version is 14.0.4762.1000

the client PROD version is 14.0.6112.5000

I get the sql script with sql profile from both system, they are slightly different:

the scripts from my DEV starts with "exec sp_executesql N'  SELECT TOP(@NUMROWS) t2.[tp_Created] AS c3c8, t1.[Type] AS c9, t3.[nvarchar6] AS c11c7, t1.[SortBehavior] AS c0, "

While the scripts from client PROD starts with "exec sp_executesql N'DECLARE @DocParentIdForRF uniqueidentifier SELECT TOP 1 @DocParentIdForRF = Docs.Id FROM Docs WHERE Docs.SiteId = @SITEID AND Docs.DirName = @FDN AND Docs.LeafName = @FLN;               SELECT TOP(@NUMROWS) t2.[tp_Created] AS c3c8, t1.[Type] AS c9, t3.[nvarchar6] AS c11c7, t1.[SortBehavior] AS c0,"

Can this be a bug that has already been fixed?

Another thing about the client PROD is the document library is relatively large, but the list throttle has been changed:


Tuesday, July 11, 2017 10:07 AM

update: in the client's test environment, which have the same(i think) list schema and similar list data,  the query with contenttypeid works, it also has build version 14.0.6112.5000, and i changed the throttle to the same as PROD.

The SQL script from client's test is the same as my DEV. so, the problem seems to be why the SQL script from the client PROD is different and the query with contenttypeid failed.


Tuesday, July 11, 2017 10:30 AM

update: tried again on the PROD, it works now, i changed nothing! now, the sql scripts i catch with sql profile are all the same, no such staff as  @DocParentIdForRF.

By the way, the powershell i use is as follows:

 $q2=new-object microsoft.sharepoint.spquery

 $q2.viewfields="<FieldRef Name='Name'/>"

 $q2.Query="<Where><BeginsWith><FieldRef Name='ContentTypeId'/><Value Type='ContentTypeId'>0x0120</Value></BeginsWith></Where>"

$q2.ViewAttributes = "Scope='RecursiveAll'";

$w.get-spweb http://servername/path

$doc=$w.lists["doclibraryname"]

$doc.getitems($q2).count


Wednesday, July 12, 2017 2:43 AM

update: if I use "Scope='Recursive'" instead of "Scope='RecursiveAll'", I will get 0 with $doc.getitems($q2).count, since according to http://blog.csdn.net/gamewyd/article/details/7374674 , Recursive only return files, RecursiveAll return both file and folders.

However, I still cannot reproduce the sql scripts I get initial from the client PROD which start with "exec sp_executesql N'DECLARE @DocParentIdForRF uniqueidentifier SELECT TOP 1 @DocParentIdForRF = Docs.Id"