Monday, February 22, 2010

Count Forms in each List in a Document Library

Code Writes output a Directory with a file name in C:\\ public string ListCount() { string logFileName = DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString(); logFileName = logFileName.Replace("/", "-"); logFileName = logFileName.Replace(":", "-"); logFileName = "C:\\Document Library Items Count - " + logFileName + ".csv"; SPSite mySite = new SPSite(SPContext.Current.Site.Url); SPWeb myWeb = mySite.OpenWeb(); StringBuilder tempTitle = new StringBuilder(""); StringBuilder tempItemCount = new StringBuilder(""); foreach (SPList list in myWeb.Lists) { string BTemplate = list.BaseTemplate.ToString(); if (list.BaseType == SPBaseType.DocumentLibrary && BTemplate == "XMLForm") { int FolderCount = list.Folders.Count; tempTitle.Append(list.Title + ","); tempTitle.Append("\t"); tempTitle.Append((list.ItemCount - FolderCount)); tempTitle.AppendLine(); if (FolderCount != 0) { list.RootFolder.SubFolders.Count.ToString(); foreach (SPListItem myList in list.Folders) { string FolderName = myList.DisplayName.ToUpper(); tempTitle.Append(" ( Folder: " + FolderName + " in " + myList.Folder.ParentFolder.Name + " )"); SPFolder folder = myWeb.GetFolder(myList.DisplayName); string folderItemsCount = myList.Folder.Files.Count.ToString(); tempTitle.Append(" Number of Forms : " + folderItemsCount); tempTitle.AppendLine(); } } } } StreamWriter logWriter = File.CreateText(logFileName); logWriter.WriteLine(""); logWriter.WriteLine("DOCUMENT LIBRARY NAME" + "," + "NUMBER OF FORMS"); logWriter.WriteLine(""); logWriter.WriteLine(tempTitle); logWriter.Close(); return (tempTitle.ToString()); }

No comments: