Feb 1, 2011

Beware of SPWeb.GetFolder(URL)

After a call to SPWeb.GetFolder(URL), you better check if it exists by spFolder.Exists(), since the GetFolder() call won't return error even if the folder doesn't exist. What is more, even if it doesn't exist, you can still get some properties (like spFolder.Url etc) out of it. But all of sudden, you get the following error when you try to get Item out of the folder object:


The object specified does not belong to a list. at Microsoft.SharePoint.SPWeb.GetItem(String strUrl, Boolean bFile, Boolean cacheRowsetAndId)


at Microsoft.SharePoint.SPFolder.get_Item()

This seems to me a bug, but is documented in http://msdn.microsoft.com/en-us/library/ms461547.aspx

If the folder does not exist, an SPFolder object is returned, but any attempt to access its properties throws a FileNotFoundException exception.