A Practical Guide to SharePoint 2013

A Practical Guide to SharePoint 2013
A Practical Guide to SharePoint 2013 - Book by Saifullah Shafiq

Wednesday, May 20, 2009

Small SPQuery tip (How to get rid of "Cannot complete this action" error)

You try to run an SPQuery and get a "Cannot complete this action" error. I have seen users who think it's a permissions issue and they try to use elevatedprivileges. Remember, if you get this error when running an SPQuery object,the first thing you should look for is the query itself.  Because of a small semantical mistake the query will not run and SharePoint will throw this error. These mistakes are often difficult to locate with eye. I'd suggest use a tool, such as U2U CAML builder, to construct queries. Here is an example:
 string sQuery =
                   "<BR /><Where>" +
                    "<And>" +
                      "<Eq>" +
                         "<FieldRef Name='Title' />" +
                         "<Value Type='Text'>" + sDocumentName.ToString().Trim() + "</Value>" +
                      "</Eq>" +
                      "<Eq>" +
                      "<FieldRef Name='fc5bb16d_x002d_5336_x002d_45a6_x002d_b6e5_x002d_0c9e8de73e26' />" +
                         "<Value Type='Text'>" + sActivity + "</Value>" +
                      "</Eq>" +
                    "</And" +
                   "</Where>";
This query looks fine when you see it and it should run but it won't run because it has a small error.  If you notice the closing AND tag, you'll the closing angular bracket is missing. The query will compile but SharePoint will throw an error. The "</And" should have been "</And>".