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 ;-)

5 comments:

Tom Van Gaever said...

Nice article, it sure helped me revealing the secrets of the client object model with an external list.

sukumar said...

Thanks for sharing about SharePoint. that post useful for can get CamlQuery external list. Sharepoint Development

Anonymous said...

Amazingly helpful thanks for sharing.

LosT_UseR said...

Really great work there!
Thanks...

digital signature software said...

Hi there! glad to drop by your page and found these very interesting and informative stuff. Thanks for sharing, keep it up!