SPQuery Error: Microsoft.SharePoint.SPException was unhandled by user code. Cannot complete this action. Please try again.
Posted
Tuesday, January 26, 2010 3:05 PM
by
CoreyRoth
When I get an error, I like to blog about it. Especially when the error gives you no useful information whatsoever. Now, you can get the above error in a variety of ways, but the one I am going to discuss today is when using SPQuery. I inherited some code that had some CAML queries in it and I could not figure out what the cause was at first. When I called SPList.GetItems(SPQuery), I would receive something like the following.
Microsoft.SharePoint.SPException was unhandled by user code
Message="Cannot complete this action.\n\nPlease try again."
Source="Microsoft.SharePoint"
ErrorCode=-2147467259
StackTrace:
at Microsoft.SharePoint.Library.SPRequest.GetListItemDataWithCallback(String bstrUrl, String bstrListName, String bstrViewName, String bstrViewXml, SAFEARRAYFLAGS fSafeArrayFlags, ISP2DSafeArrayWriter pSACallback, ISPDataCallback pPagingCallback, ISPDataCallback pSchemaCallback)
at Microsoft.SharePoint.SPListItemCollection.EnsureListItemsData()
at Microsoft.SharePoint.SPListItemCollection.get_Count()
at Samson.P2S.SharePoint.Services.Synchronization.P2S_SynchronizationService.<>c__DisplayClass6.<GetInventoryChecklist>b__4() in C:\Projects\SamsonTFS\P2S\P2S_V0\Samson.P2S.SharePoint.Services.Synchronization\P2S_SynchronizationService.asmx.cs:line 166
at Microsoft.SharePoint.SPSecurity.CodeToRunElevatedWrapper(Object state)
at Microsoft.SharePoint.SPSecurity.<>c__DisplayClass4.<RunWithElevatedPrivileges>b__2()
at Microsoft.SharePoint.Utilities.SecurityContext.RunAsProcess(CodeToRunElevated secureCode)
InnerException: System.Runtime.InteropServices.COMException
Message="Cannot complete this action.\n\nPlease try again."
Source=""
ErrorCode=-2147467259
StackTrace:
at Microsoft.SharePoint.Library.SPRequestInternalClass.GetListItemDataWithCallback(String bstrUrl, String bstrListName, String bstrViewName, String bstrViewXml, SAFEARRAYFLAGS fSafeArrayFlags, ISP2DSafeArrayWriter pSACallback, ISPDataCallback pPagingCallback, ISPDataCallback pSchemaCallback)
at Microsoft.SharePoint.Library.SPRequest.GetListItemDataWithCallback(String bstrUrl, String bstrListName, String bstrViewName, String bstrViewXml, SAFEARRAYFLAGS fSafeArrayFlags, ISP2DSafeArrayWriter pSACallback, ISPDataCallback pPagingCallback, ISPDataCallback pSchemaCallback)
InnerException:
The cause is actually simple. My CAML query was malformed. Specifically my where clause was not nested properly. The previous developer put three conditions in an and clause and that simply does not work. A COM exception though? Really? Wouldn’t it have just been better if it validated the query first and gave us a nice error message such as the following?
CAML query is malformed.
That would be too nice I guess. Anyhow, if you get this when querying, start looking at that query. While, I am here, I’ll remind you if you copy the query from CAML Query Builder, to remove the Query element. If you can’t figure it out and first, go back to the CAML query building tool of your choice and start executing the query there and comparing it to the one you have. You should be up and running in no time.