in

Dot Net Mafia

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

This Blog

Syndication

Archives

Corey Roth [MVP]

A SharePoint MVP bringing you the latest time saving tips for Ionic, SharePoint, and Office 365.

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

 

Eliza Sahoo said:

For instance, let us take a State List which holds all the states of India. States are grouped under different zones i.e. North, South, East & West. If I need to retrieve all the States for North zone then my SP Query would look somewhat like,

SPQuery stateQuery = new SPQuery();

stateQuery.Query =

"<Where><Eq><FieldRef Name=\"Zone\"/><Value Type=\"Text\">North</Value></Eq></Where>";

SPListItemCollection stateCol = StateList.GetItems(stateQuery);

In the code above the StateCollection object gets populated with the States only related to North zone. You can then bind this collection to a GridView or DataList or any other similar controls to view the actual values.

www.mindfiresolutions.com/Sharepoint-SPQuery-30.php

May 6, 2010 7:48 AM
 

Ahmad Nawaz said:

Thank you.

Its really help me to solve the error.

May 13, 2010 1:25 AM
 

Ashish said:

Thank you! I was writing the wrong case (<eq> </eq>) in my CAML query. It's actually <Eq> </Eq>...

Thanks again! It saved my day!

November 2, 2010 3:47 PM
 

Haider said:

Please give  some more clues to make a (large) proper working query using CAML bulider. as it may contain a lot of nested elements. in my case CAML query contains many elements. it executes successfully on CAML query builder but is problematic in code.

just eliminating <Query></Query> from a CAML query builder generated query is enough or we need to look at the syntax too?

June 21, 2013 3:39 PM
 

CoreyRoth said:

@Haider usually eliminating the Query element is enough.

July 16, 2013 10:59 AM

Leave a Comment

(required)  
(optional)
(required)  
Add

About CoreyRoth

Corey Roth is an independent SharePoint consultant specializing in ECM, Apps, and Search.
2018 dotnetmafia.
Powered by Community Server (Non-Commercial Edition), by Telligent Systems