Monday, December 26, 2011

Document version size in Sharepoint

The below code will calculate the document version size for each item for a single document library.

using (SPSite site = new SPSite("http://Site_URL/")) //Specify your SiteURL
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList objList = web.Lists["Doclib"];//Document library Name
                    SPQuery objQuery = new SPQuery();
                   
                    objQuery.ViewAttributes = "Scope=\"Recursive\"";

                    SPListItemCollection objItemColl = objList.GetItems(objQuery);
                    foreach (SPListItem objItem in objItemColl)
                    {
                        long versionSize = 0;

                        SPListItemVersionCollection objVerisionColl = objItem.Versions;

                        foreach (SPListItemVersion objVersion in objVerisionColl)
                        {
                            int versionID = objVersion.VersionId;
                            string strVersionLabel = objVersion.VersionLabel;

                            SPListItem objLstItm = objVersion.ListItem;
                            versionSize = versionSize + objLstItm.File.Length;
                           
                        }
                        Console.WriteLine("Item:" + objItem.Name + " Version Count:" + objVerisionColl.Count + " Size:" + versionSize.ToString());
                       

                    }
                }
            }

No comments:

Post a Comment