Negative item count in document libraries

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.

 

      string siteUrl = "http://moss/site";

      using (SPSite site = new SPSite(siteUrl))

      {

        using (_web = site.OpenWeb())

        {

          SPList list = _web.Lists["CopyTest"];

          Console.WriteLine("Items.Count:{0}, ItemCount:{1}", list.Items.Count, list.ItemCount);

 

          Console.WriteLine("Creating 1.0 folder … ");

          SPFolder rootFolder = _web.GetFolder("/CopyTest");

 

          SPFolder version1Folder = rootFolder.SubFolders.Add("/CopyTest/1.0");

          version1Folder.Update();

          list.Update();

          Console.WriteLine("OK");

          Console.WriteLine("Items.Count:{0}, ItemCount:{1}", list.Items.Count, list.ItemCount);

 

          Console.WriteLine("Creating Test1 in 1.0 folder … ");

          SPFolder testFolder = version1Folder.SubFolders.Add(version1Folder.Url + "/Test1");

          testFolder.Update();

          list.Update();

          Console.WriteLine("OK");

          Console.WriteLine("Items.Count: {0}", list.Items.Count);

          Console.WriteLine("ItemCount: {0}", list.ItemCount);

 

          Console.WriteLine("Creating Test1.1 in Test1 folder … ");

          SPFolder test2Folder = version1Folder.SubFolders.Add(version1Folder.Url + "/Test1/Test1.1");

          test2Folder.Update();

          list.Update();

          Console.WriteLine("OK");

          Console.WriteLine("Items.Count: {0}", list.Items.Count);

          Console.WriteLine("ItemCount: {0}", list.ItemCount);

 

          Console.WriteLine("Creating Test2 in 1.0 folder … ");

          SPFolder test3Folder = version1Folder.SubFolders.Add(version1Folder.Url + "/Test2");

          test3Folder.Update();

          list.Update();

          Console.WriteLine("OK");

          Console.WriteLine("Items.Count: {0}", list.Items.Count);

          Console.WriteLine("ItemCount: {0}", list.ItemCount);

 

          Console.WriteLine("Creating 2.0 folder in the root … ");

          SPFolder version2Folder = rootFolder.SubFolders.Add("/CopyTest/2.0");

          version2Folder.Update();

          list.Update();

          Console.WriteLine("OK");

          Console.WriteLine("Items.Count:{0}, ItemCount:{1}", list.Items.Count, list.ItemCount);

         

          Console.WriteLine("Copy 1.0 items to 2.0");

         

          foreach (SPFolder subFolder in version1Folder.SubFolders)

          {

            Console.WriteLine("Copy {0} folder … ", subFolder.Name);

            subFolder.CopyTo(version2Folder.Url + "/" + subFolder.Name);

            SPFolder folder = _web.GetFolder(version2Folder.Url + "/" + subFolder.Name);

            if(folder.Exists)

              Console.WriteLine("Folder exists, url is: {0}", folder.ServerRelativeUrl);

            folder.Update();

            list.Update();

            Console.WriteLine("OK");

            Console.WriteLine("Items.Count:{0}, ItemCount:{1}", list.Items.Count, list.ItemCount);

          }

         

          Console.WriteLine("Deleting 1.0 folder … ");

          SPFolder deleteFolder = _web.GetFolder("/CopyTest/1.0");

          if (deleteFolder.Exists)

          {

            deleteFolder.Delete();

            list.Update();

            Console.WriteLine("OK");

          }

          Console.WriteLine("Deleting 2.0 folder … ");

          deleteFolder = _web.GetFolder("/CopyTest/2.0");

          if (deleteFolder.Exists)

          {

            deleteFolder.Delete();

            list.Update();

            Console.WriteLine("OK");

          }

 

          Console.WriteLine("SPList.ItemCount = {0}", list.ItemCount);

        }

 

      }

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 int he  ItemCount does not changed until SPList.Update() is called. You should be aware of this behavior in your custom applications.

2 Responses to “Negative item count in document libraries”

  1. liam says:

    Did you ever get a response about this? I am also getting this behavior.

  2. pholpar says:

    Yes, after about a year of conversation we got the response that this issue is to complex and deep in the corre of the product that it won’t be fixed in WSS 3.0. :-(

Leave a Reply