Wednesday, September 23, 2009

CAML Queries - Cannot complete action

The Collaborative Application Markup Language (better known as CAML) is an XML-based query language that helps you querying, building and customizing Web sites based on Windows SharePoint Services. The XML elements define various aspects of a WSS site.

The Ground Rule should be either or join operators can only validate two expressions. Once it has more than two expressions in a single or join operator, it cannot be evaluated and throws a strange exception to the users. So, split them in to multiple expressions as below.

General (small example) would throw 'Cannot complete this action' exception:
< "Where" >
< "Or" >
< "Eq" >< name="'Country'/">< type="'LookUp'">India< /Value >< /Eq >
< "Eq" >< name="'Country'/">< type="'LookUp'">China< /Value >< /Eq >
< "Eq" >< name="'Country'/">< type="'LookUp'">Germany< /Value >< /Eq>
< "Eq" >< name="'Country'/">< type="'LookUp'">USA< /Value >< /Eq >
< /Or >
< /Where >

Optimized and to be working:
< "Where" >
< "Or" >
< "Or" >
< "Or" >
< "Eq" >< name="'Country'/">< type="'LookUp'">India< /Value >< /Eq >
< "Eq" >< name="'Country'/">< type="'LookUp'">China< /Value >< /Eq >
< /Or >
< "Eq" >< name="'Country'/">< type="'LookUp'">Germany< /Value >< /Eq>
< /Or >
< "Eq" >< name="'Country'/">< type="'LookUp'">USA< /Value >< /Eq >
< /Or >
< /Where >

In the above working example, you could observe that the join operator has only two expressions at any point of time.

** "Where" should be replaced with Where and "Or" should be replaced with Or. And also spaces shouldn't be there after and before the less than and greater than symbols. As blogger has a limitation, had to place like that.

So, should be very careful while writing CAML query with more values.

Microsoft has to keep an eye on throwing the right error message to the users.

You could also find the above info here

Other Sources:

Happy Coding...!!

~ Gangadhar Kotu

No comments: