Tuesday, July 27, 2010

SPSiteDataQuery error on Discussion Board List

SPSiteDataQuery is used to get the data from multiple list with the same type. The code below shows how to get all items in Discussion Board List. Only show Subject and ID after returning data.

SPSiteDataQuery query = new SPSiteDataQuery();


query.ViewFields = "<FieldRef Name=\"LinkDiscussionTitle\"/><FieldRef Name=\"ID\"/>";
query.Lists = "<Lists ServerTemplate=\"108\"/>";


DataTable table = web.GetSiteData(query);

But you will get an error like this:


AAAAA.Data.UnitTest.GetInternalName.GetDataFromDB : Microsoft.SharePoint.SPException : Exception occurred. (Exception from HRESULT: 0x80020009 (DISP_E_EXCEPTION))
  ----> System.Runtime.InteropServices.COMException : Exception occurred. (Exception from HRESULT: 0x80020009 (DISP_E_EXCEPTION))


The details:



 at Microsoft.SharePoint.Library.SPRequest.CrossListQuery(String bstrUrl, String bstrXmlWebs, String bstrXmlLists, String bstrXmlQuery, ISP2DSafeArrayWriter pCallback, Object& pvarColumns)
   at Microsoft.SharePoint.SPWeb.GetSiteData(SPSiteDataQuery query)
   at Bamboo.Data.UnitTest.GetInternalName.GetDataFromDB() in D:\BambooFolder\TFS2008\Bamboo.WP.ListConsolidator\Bamboo.ListConsolidatorSoln\Bamboo.Data.UnitTest\GetInternalName.cs:line 168
--COMException
   at Microsoft.SharePoint.Library.SPRequestInternalClass.CrossListQuery(String bstrUrl, String bstrXmlWebs, String bstrXmlLists, String bstrXmlQuery, ISP2DSafeArrayWriter pCallback, Object& pvarColumns)
   at Microsoft.SharePoint.Library.SPRequest.CrossListQuery(String bstrUrl, String bstrXmlWebs, String bstrXmlLists, String bstrXmlQuery, ISP2DSafeArrayWriter pCallback, Object& pvarColumns)


The LinkDiscussionTitle is one of the internalname of Discussion Board List, but if you try to query by that column, you will get an error. You can query by any column, except for LinkDiscussionTitle. Just remove that column out your query string, it works fine.

SPSiteDataQuery has been supported all functions like SPQuery exception for GroupBy, if you try to put the GroupBy clause into query, it will throw exception but you can work with filtering and sorting.

No comments: