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

Is it possible to order by content type in a CAML query?

I seem to always get the same result no matter what I do with the Order by. Ordering by ID instead works perfectly.

query.Query = @"
    <Where>
        <And>
            <BeginsWith>
                <FieldRef Name='ContentTypeId' />
                <Value Type='ContentTypeId'>0x010039EFCF9C11783C409E74551800AB7525</Value>
            </BeginsWith>
            <Eq>
                <FieldRef Name='SectionTitle' />
                <Value Type='Lookup'>" + filter + @"</Value>
            </Eq>
        </And>
    </Where>
    <OrderBy Override='TRUE'>
        <FieldRef Name='ContentType' Ascending='FALSE' />
    </OrderBy>";
share|improve this question

2 Answers 2

up vote 5 down vote accepted

That looks like it should work. I know it's not optimal but, as an alternative, you could do the sorting on the client side.

var items = list.GetItems(query).OfType<SPListItem>().OrderByDescending(i => i["ContentType"]);
share|improve this answer

My understanding is that the schema of the list and fields defines whether or not a field is sortable. For ContentType field "Sortable" (that's a boolean attribute) must have been set to False.

That's the reason it is not available when you create view for your list; the field is not available in the dropdown. Same restrictions would apply to SPQuery also.

share|improve this answer

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.