Wednesday, March 31, 2010

SharePoint 2010: CamlQuery on External List

When running a CamlQuery on an External List, we got an error "The given key was not present in the dictionary.".

Apparently you have to specify the <ViewFields> tag in the CamlQuery.ViewXml property. So you have to specify the <ViewFields></ViewFields> tag with the right fields in the ViewXml property.

For example, this will NOT work:
camlQuery.ViewXml = @"
<Method Name='ReadList'/>
<Query>
<Where>
<Contains>
<FieldRef Name='Name'/>
<Value Type='Text'>AAS</Value>
</Contains>
</Where>
<OrderBy>
<FieldRef Name='Name'/>
</OrderBy>
</Query>
</View>";


... but this will work:
camlQuery.ViewXml = @"
<Method Name='ReadList'/>
<Query>
<Where>
<Contains>
<FieldRef Name='Name'/>
<Value Type='Text'>AAS</Value>
</Contains>
</Where>
<OrderBy>
<FieldRef Name='Name'/>
</OrderBy>
</Query>
<ViewFields>
<FieldRef Name='AccountID' ListItemMenu='TRUE' LinkToItem='TRUE'/>
<FieldRef Name='Name'/>
</ViewFields>


</View>";


To find out the XML you have to use for the <ViewFields> tag, you can add a ListView webpart of your External List to a page and grab the <ViewFields> XML by opening the page in SharePoint Designer:


I read this solution here: http://blog.trivadis.com/blogs/stefanfrutiger/archive/2010/03/15/sharepoint-2010-bcs-zugriff-auf-externe-daten-aus-einer-silverlight-4-applikation.aspx. Luckely, I understand a little bit German ;-)