in

Dot Net Mafia

Group site for developer blogs dealing with (usually) .NET, SharePoint 2010, and other Microsoft products, as well as some discussion of general programming related concepts.

This Blog

Syndication

Corey Roth - DotNetMafia.com - Tip of the Day

Bringing you the latest time saving tips for SharePoint 2010, MOSS 2007, ASP.NET, LINQ, and Visual Studio 2010

SPQuery Error: Microsoft.SharePoint.SPException was unhandled by user code. Cannot complete this action. Please try again.

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.

Published Jan 26 2010, 03:05 PM by CoreyRoth
Filed under: , ,

Comments

No Comments

Leave a Comment

(required)  
(optional)
(required)  
Add

About CoreyRoth

Corey Roth is a SharePoint Consultant for Stonebridge, Inc. specializing in clients in the Energy Sector.
2009 dotnetmafia.
Powered by Community Server (Non-Commercial Edition), by Telligent Systems