Quantcast
Channel: SharePoint 2010 - Development and Programming forum
Viewing all articles
Browse latest Browse all 11571

CAML Joins using SPQuery - ProjectedFields ruins everything!

$
0
0

Hi guys, I'm repressing the urge to start throwing things round my office at the moment.

I've set up a simple set of lookups, from an 'Item' list, which has a lookup to a 'Cities' list, which has a lookup to a 'Countries' list. I execute the following code:

SPQuery query = new SPQuery();
query.ViewFields = FormatXml(this.ViewFieldsCaml);
query.Query = FormatXml(this.QueryCaml);
query.Joins = FormatXml(this.JoinsCaml);
query.ProjectedFields = FormatXml(this.ProjectedFieldsCaml);// query the 'Items' list for the data
SPListItemCollection itemCollection = this.SourceList.GetItems(query);
DataTable data = itemCollection.GetDataTable();

SourceList is my 'Item' list - this is returned fine.
FormatXml removes white space from my XML.

The following CAML is used:

QueryCaml

<Where/><OrderBy/>

ViewFieldsCaml

<FieldRefName="ID"/><FieldRefName="Title"/>

If run at this point, the above code successfully retrieves all of the items in the 'Item' list, as expected. So I add the CAML for my joins:

JoinsCaml

<JoinType="INNER"ListAlias="city"><Eq><FieldRefName="City"RefType="Id"/><FieldRefList="city"Name="ID"/></Eq></Join><JoinType="INNER"ListAlias="country"><Eq><FieldRefList="city"Name="Country"RefType="Id"/><FieldRefList="country"Name="ID"/></Eq></Join>

Again, everything works as expected - I've joined my lists with an INNER join so all items without a specified City disappear from the returned DataTable (as expected). So now it's time to add my projected fields and reference them in my view fields...

ProjectedFieldsCaml

<FieldName="CityName"List="city"Type="Lookup"ShowField="Title"/><FieldName="CountryName"List="country"Type="Lookup"ShowField="Title"/>

Execute at this point and BOOM!!!!! Before I've even added CityName and CountryName to my ViewFields the thing fails. The SourceList.GetItems(SPQuery) runs fine, but itemCollection.GetDataTable() fails with a NullReferenceException and the following stack trace:

Object reference not set to an instance of an object.
 at Microsoft.SharePoint.SPFieldMap.EnsureFieldArray()
 at Microsoft.SharePoint.SPFieldMap.GetFieldObject(Int32 columnNumber)
 at Microsoft.SharePoint.SPListItemCollection.GetVisibleFieldIndices(Boolean isJsGrid, Int32[]& arrVisibleFieldIndices, Int32& iVisibleFieldCount)
 at Microsoft.SharePoint.SPListItemCollection.GetDataTableCore(Boolean isJsGrid)
 at Acer.Sharepoint.Core.QueryView.ListQueryViewWebPart.SelectData()
 at Acer.Sharepoint.Core.QueryView.BaseQueryViewWebPart.EnsureData()
I'm using SharePoint 2010 Foundation on a Windows 7 Professional dev box. Any clues before I start causing serious damage to work property?!?!?!?

Viewing all articles
Browse latest Browse all 11571

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>