SPSiteDataQuery throwing exception while setting it's Query property

1

I am querying multiple task lists in a SharePoint site. I can retrieve the results until I set the Query property and specify the OrderBy clause. The detailed exception and code is pasted below. Appreciate any help

   private void doTasks(SPWeb currentWeb, SPSiteDataQuery q)
    {

        q.Query =
             "<OrderBy>" +
             "   <FieldRef Name='Priority' />" +
             "   <FieldRef Name='DueDate' />" +
             "</OrderBy>";


        // Specify the view fields.
        q.ViewFields = "<FieldRef Name='Title' Type='Text'/>";
        q.ViewFields += "<FieldRef Name='AssignedTo' Type='User' Nullable='TRUE' />";
        q.ViewFields += "<FieldRef Name='PercentComplete' Type='Number' Nullable='TRUE'/>";
        results = currentWeb.GetSiteData(q);

        if (results.Rows.Count > 0)
        {
                 //blah!
        }
    }

[COMException (0x80020009): 0x80020009] Microsoft.SharePoint.Library.SPRequestInternalClass.CrossListQuery(String bstrUrl, String bstrXmlWebs, String bstrXmlLists, String bstrXmlQuery, ISP2DSafeArrayWriter pCallback, Object& pvarColumns) +0 Microsoft.SharePoint.Library.SPRequest.CrossListQuery(String bstrUrl, String bstrXmlWebs, String bstrXmlLists, String bstrXmlQuery, ISP2DSafeArrayWriter pCallback, Object& pvarColumns) +174

[SPException] Microsoft.SharePoint.SPGlobal.HandleComException(COMException comEx) +27428978 Microsoft.SharePoint.Library.SPRequest.CrossListQuery(String bstrUrl, String bstrXmlWebs, String bstrXmlLists, String bstrXmlQuery, ISP2DSafeArrayWriter pCallback, Object& pvarColumns) +27812419 Microsoft.SharePoint.SPWeb.GetSiteData(SPSiteDataQuery query) +521 StoraEnso.Collaboration.WebParts.SubSiteItemAggregator.SubSiteItemAggregator.doTasks(SPWeb currentWeb, SPSiteDataQuery q) +189 StoraEnso.Collaboration.WebParts.SubSiteItemAggregator.SubSiteItemAggregator.Render(HtmlTextWriter writer) +441 Microsoft.SharePoint.WebPartPages.SPChrome.RenderPartContents(HtmlTextWriter output, WebPart part) +43

[WebPartException] Microsoft.SharePoint.WebPartPages.SPChrome.RenderPartContents(HtmlTextWriter output, WebPart part) +19826502 Microsoft.SharePoint.WebPartPages.SPChrome.RenderWebPart(HtmlTextWriter output, WebPart part) +64 Microsoft.SharePoint.WebPartPages.WebPartZone.RenderZoneCell(HtmlTextWriter output, Boolean bMoreParts, WebPart part) +1997 Microsoft.SharePoint.WebPartPages.WebPartZone.RenderWebParts(HtmlTextWriter output, ArrayList webParts) +508 Microsoft.SharePoint.WebPartPages.WebPartZone.Render(HtmlTextWriter output) +1000 System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +240 System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +240 System.Web.UI.HtmlControls.HtmlContainerControl.Render(HtmlTextWriter writer) +42 System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +240 System.Web.UI.HtmlControls.HtmlForm.RenderChildren(HtmlTextWriter writer) +253 System.Web.UI.HtmlControls.HtmlForm.Render(HtmlTextWriter output) +87 System.Web.UI.HtmlControls.HtmlForm.RenderControl(HtmlTextWriter writer) +53 System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +240 System.Web.UI.HtmlControls.HtmlContainerControl.Render(HtmlTextWriter writer) +42 System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +240 System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +240 System.Web.UI.Page.Render(HtmlTextWriter writer) +38 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +4240

sharepoint
asked on Stack Overflow Aug 26, 2011 by code master • edited Aug 30, 2011 by code master

4 Answers

2

During the migration some page layouts were using the SPSiteDataQuery to query the ContentTypes across different webs to get the content. The code worked fine in SP2007 but in SP2010 it was throwing exception as mentioned below:

Solution

Make sure the field your using for the order by is included in the SPSiteDataQuery.ViewFields property. As simple as that!

answered on Stack Overflow Sep 4, 2011 by code master
0

Wrap the OrderBy clause in <Query></Query> .

answered on Stack Overflow Aug 26, 2011 by Madhur Ahuja
0

Does it work if you remove the AssignedTo and PercentComplete FieldRefs? I ask because if you are querying all lists in a site, most will not contain AssignedTo and PercentComplete fields. This can cause an error.

What is the value of q.Lists? If you have not set q.Lists, try:

q.Lists = "<Lists ServerTemplate=\"107\" />"
answered on Stack Overflow Aug 29, 2011 by Rich Bennema
0

If anyone else is having a similar issue: I've spent two hours before I found out that the last line of the CAML query cannot be a blank line. In other words, there must not be any line breaks at the end of the CAML query. Removing the line breaks solved the problem immediately.

answered on Stack Overflow Sep 9, 2016 by (unknown user)

User contributions licensed under CC BY-SA 3.0