Wednesday, January 18, 2012

SPSiteDataQuery not returning all results when using ContentType filter

Recently, I was working on a case where all the SPListItems using a specific content type needs to be listed from the whole site collection. As you all know, SPSiteDataQuery is the way to go.

As I was getting to the end I noticed it was not returning all the items from the whole site collection. Instead it is returning only a few items, more like list items only from one particular list (weird!!! right) and no errors as well. If the same web part is added in some specific inner webs and tried to get all teh items from current web and all the sub webs the query works perfectly well. I was totally confused ...

After some researches, I came across this thread (http://social.technet.microsoft.com/Forums/en/sharepoint2010programming/thread/1294669a-546d-44f1-8b7d-6972bc11bc34) where it said the SPSiteDataQuery has some issues with filtering with "Computed" fields such as "ContentType" field. So I changed the filter from "ContentType" to "ContentTypeID" which f type "ContentTypeId" which seems to be working perfectly fine.

ContentType Filter:

<Where>
    <Eq>
        <FieldRef Name="ContentType" />
        <Value Type="Computed">My Content Type</Value>
    </Eq>
</Where>

ConteTypeId Filter:

<Where>
    <BeginsWith>
        <FieldRef Name="ContentTypeId" />
        <Value Type="ContentTypeId">My Content Type</Value>
    </BeginsWith>
</Where>

Above filter goes for the ".Query" attribute of the SPSiteDataQuery to filter by ContentType name and ContentTypeID respectively.

Hope this helps someone facing the same issue.

Thanks
Senthil S

No comments:

Post a Comment