1

I have a custom content type. In this content type, I have a text column (Mycolumn).

In a site collection, I have several instances of list bound to this content type.

Is there a way to retrieve all values of this column in all the site collection?

My goal is to build a webpart that suggest, in a drop down list, all existing value, in order to build a CAML query and display results.

I'm using SP2010 Standard, and a search server is setup (if it can helps).

2 Answers 2

1

The SPContentTypeUseage class may be of help to you here. It has a static method GetUsages(SPContentType) that will return a list of SPContentTypeUsage objects. Through those objects, you can determine the specific lists that use your content type, open the lists, and get all the values of your column.

1
  • I was hoping that there some kind of magical method that can do the job. Your suggestion will help me anyways. Thanks. I'll have to implement a bit of cache, but this will be a good first step.
    – Steve B
    Commented Jan 14, 2014 at 8:56
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.