Here is a possible solution to solve the problem:
private IEnumerable<T> GetDistinctValues<T>(SPSite site, int listTemplateId, SPContentTypeId contentType, string fieldName)
{
var query = new SPSiteDataQuery
{
Lists = "<Lists ServerTemplate='" + listTemplateId + "' />",
ViewFields = "<FieldRef Name='" + fieldName + "' Type='Text'/>",
Query = "<Where><BeginsWith><FieldRef Name='ContentTypeId' /><Value Type='ContentTypeId'>" + SyndicateNewsContentTypeId + "</Value></BeginsWith></Where>" +
"<OrderBy><FieldRef Name='" + fieldName + "' /></OrderBy>",
Webs = "<Webs Scope='SiteCollection' />"
};
var result = site.RootWeb.GetSiteData(query);
var resultView = new DataView(result);
var distinct = resultView.ToTable(true, fieldName);
foreach (DataRow item in distinct.Rows)
{
yield return (T)item[0];
}
}
Basically, the idea is to issue a SPSiteDataQuery
that is filtering against my custom content type. Then, I use the DataView.ToTable
method, to get distinct values.