How can I get a Content Query Web Part to show items from more than one type of list?

Mr Miller picture Mr Miller · Aug 3, 2011 · Viewed 11.5k times · Source

I am trying to use a Content Query Web Part to create a "What's New" page in Sharepoint 2010. The goal of this page is to display any documents that have been uploaded in the last 14 days. The trick is that these documents could belong to any number of lists that are defined in any number of list definitions. These are custom list definitions based on custom content types. We do have a base content type that each of our custom content types inherit from. They are also all part of the same content type group. I have determined that it is not possible to use the SP UI to set up the CQWP to return items from multiple list types, since one of the required query fields is list type. So - I'm attempting to use CAML to define this CQWP and this is where I'm having trouble. My end goal is for the CAML to be defined in a site definition (onet.xml file) but I've also not had any luck with uploading a .webpart file from the SP UI. I have found several articles that explain how this should be done but haven't been able to get the suggested solutions to work. This MSDN article tells me it should be possible using the ListsOverride element. This is how the article tells me to do it:

<![CDATA[
          <Lists BaseType="0">
          </Lists> ]]>

Since I'm looking for Document Libraries I would use a base type of 1 rather than 0.

I have been unable to determine the syntax for what that would actually look like from within the onet.xml file so I thought I'd start with trying to get it uploaded as a .webpart file.

This is what the ListsOverride element looks like:

<property name="ListsOverride" type="string">
    <![CDATA[<Lists BaseType="1"></Lists>]]>
</property>

However - as soon as I upload a .webpart file with this in the CAML and add the webpart to a page I break that page in Sharepoint. This is what I get:

Server Error in '/' Application. Attempted to use an object that has ceased to exist.

Research on that error points me to code that disposes of an object such as SPContext.Current.Web but I don't even have any code at all here. It almost looks like the CQWP has a bug in it. OR - I'm either not formatting that CAML properly or maybe I need to change something in a different element in the CAML?

I am running Sharepoint 2010 SP1 with all the latest patches. (I believe)

I've tried several other formats but without luck.

I tried getting rid of the embedded CDATA tags like this:

<property name="ListsOverride" type="string">
    <Lists BaseType="1"></Lists>
</property>

But then Sharepoint won't let me upload the .webpart file. (Invalid .webpart file)

The format with the embedded CDATA tags seems to be the only way it lets me upload the file.

I've tried (just for the sake of narrowing down my issue) to specify specific lists like this:

    <property name="ListsOverride" type="string">
<![CDATA[<Lists><List Id="{5a2f79bb-cc82-4171-88ac-65f20e7b5fa8}" /></Lists>]]>

That doesn't break the page but the webpart gives me less than useful error on the page (Unable to display this Web Part)

I'm not 100% sure that I used the appropriate GUID in that attempt. I got it from looking in the server explorer in Visual Studio (Under Lists and Libraries >> Document Libraries >> My List Type. (I got the GUID from the Id property)

I should mention that I am fairly new with Sharepoint development. I would have hoped that something seemingly so core to what Sharepoint does would be much easier than this.

Can someone please point out what I am doing wrong? Maybe I'm going about the whole thing in the wrong way? I appreciate any help that anyone can give me!

Answer

Michele Pondo picture Michele Pondo · Aug 5, 2011

I found that the issue was all the "extra" fields that SharePoint had populated.

In my case, I started by configuring the content query web part on the page and then exporting it to get the xml. Then, I put the XML in my onet.xml file of my site definition. That worked fine initially, but as I tried to configure the base types, it did not behave as expected.

If I removed all the additional fields and just used the few fields that I actually needed to configure, then it started to work. In fact, I didn't even need to use the ListsOverride element, since there is a BaseType property that works just fine. It appears that one of the other properties that I was pulling in was really the cause of my issues.

Here's a webpart definition that I used in my onet.xml:

<AllUsersWebPart WebPartZoneID="WebPartZone" WebPartOrder="1">
      <![CDATA[                
    <webParts>
      <webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
        <metaData>
          <type name="Microsoft.SharePoint.Publishing.WebControls.ContentByQueryWebPart, Microsoft.SharePoint.Publishing, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
          <importErrorMessage>Cannot import this Web Part.</importErrorMessage>
        </metaData>
        <data>
          <properties>
            <property name="Title" type="string">Recently Updated Documents</property>
          <property name="ChromeType" type="chrometype">TitleOnly</property>
          <property name="ChromeState" type="chromestate">Normal</property>
              <property name="WebUrl" type="string">~site</property>
          <property name="BaseType" type="string">1</property>
              <property name="ContentTypeBeginsWithId" type="string">0x0101008B0856395DCD40F99C9B42B6BF92BDDB</property>
          <property name="FilterField1" type="string">{28cf69c5-fa48-462a-b5cd-27b6f9d2bd5f}</property>
          <property name="FilterType1" type="string">DateTime</property>
          <property name="FilterOperator1" type="Microsoft.SharePoint.Publishing.WebControls.ContentByQueryWebPart+FilterFieldQueryOperator, Microsoft.SharePoint.Publishing, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c">Geq</property>
          <property name="FilterDisplayValue1" type="string">-14</property>
          <property name="FilterValue1" type="string">-14</property>
          <property name="SortBy" type="string">{8c06beca-0777-48f7-91c7-6da68bc07b69}</property>
          <property name="SortByFieldType" type="string">DateTime</property>
          <property name="SortByDirection" type="Microsoft.SharePoint.Publishing.WebControls.ContentByQueryWebPart+SortDirection, Microsoft.SharePoint.Publishing, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c">Desc</property>
          <property name="ItemXslLink" type="string">~sitecollection/Style Library/DealerPortal/ItemStyle.xsl</property>
          <property name="CommonViewFields" type="string">Name,Text;Created,DateTime;Modified,DateTime;Body,Note;DocumentIconImageUrl;OnClickForWebRendering</property>
        </properties>
      </data>
      </webPart>
      </webParts>             
      ]]>      

    </AllUsersWebPart>