SPWeb.GetListItem COMException on first call - second is ok

1.5k Views Asked by At

I'm trying to use SPWeb.GetListItem() to get an item by a known URL (MSDN).

So basically I'm doing the following:

using (SPSite spSite = SPContext.Current.Site)
{
    using (SPWeb spWeb = spSite.RootWeb)
    {
        SPListItem spListItem = spWeb.GetListItem
            ("/sites/testSite/Lists/testList/Folder/Subfolder");
    }
}

in case you are wondering, I'm trying to get the folder "Subfolder" to do stuff with it.

The problem is that the first time I call this, I get a COMExpeption:

Cannot complete this action.

Please try again.<nativehr>0x80004005</nativehr><nativestack></nativestack>

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Runtime.InteropServices.COMException: Cannot complete this action.

There are some reports about the COMException happening because you need to use the full relative site URL, so instead of /Lists/testList/Folder/SubFolder, you need to use /sites/testSite/Lists/... - I'm doing just that. I also tried using the absolute URL (http://sharepoint/sites/...). The problem remains: I get the COMException when trying to get the spListItem for the first time.
When I try to run the code again after the Exception, the SPListItem is received just fine. Also all subsequent calls work - only the first one fails.

AmI doing some initializing wrong or something like that?

2

There are 2 best solutions below

0
On

Perhaps try SPWeb.GetFolder() instead... but again it depends on what exactly you want to do with the Folder. Seems strange though that you're using a method used for getting list items on a Folder.

http://msdn.microsoft.com/en-us/library/ms461547.aspx

0
On

Try to instantiate the web where the list item is located - _uiserWeb.

I tryed Site.RootWeb.GetListItem(item_url) - it gave me null first time after code redeployment and was fine second time.

_item = _userWeb.GetListItem(item_url)