Note: This is a repost, the original one was published on SharePoint Blogs on March 15 , 2008
A few days ago we received a complaint from one of our customers. They found that after deleting all documents from it, a SharePoint document library that serves as the base of a custom application shows negative item count on the All Site Content page.
We checked where this value comes from and found it is the ItemCount property of the SPList object.
An additional side effect of this strange behavior is that the indexing fails because it cannot cast the negative value to an unsigned integer (UInt32).
We could reproduce the behavior using the following code. It assumes that the address of your site is your site is http://moss/site and the name of the document library is CopyTest. The code creates a folder and subfolders, copies it using the CopyTo() method of SPFolder class, and finally deletes the folders.
We escalated the issue to MS support and waiting for a resolution now.
I should note that there is an interesting blog post about the relation of SPList.ItemCount and SPList.Items.Count here. The main point is that if the number of items is changed (for example, by deleting or creating items) in the document libraries, then the value stored in the ItemCount does not changed until SPList.Update() is called. You should be aware of this behavior in your custom applications.
Update: After about a year of mailing we got the response from the support that the behavior wouldn’t be fixed in this version, since it is too complex.