Reza on blogging [MVP]

THIS BLOG HAS MOVED TO: http://blogs.devhorizon.com/reza

Subscriptions

<January 2009>
SuMoTuWeThFrSa
28293031123
45678910
11121314151617
18192021222324
25262728293031
1234567

News



toronto.sharepoint.camp


Navigation

Post Categories

Other Bloggers

Personal Links

Monday, October 23, 2006 - Posts

How to get the size of attachments in list items using SharePoint object model Rated Excellent [5 out of 5].

I have recently written a report generator for a client that enumerates all the items in WSS lists of a web site and then attempts to count them and calculate the overall size. Unlike many reporting tools out there which only go through list items in document libraries and ultimately through document libraries + picture libraries, my reporting tool, iterate through all the lists of any kind. It goes through all the attachments in attachment-enabled lists like links, Announsment, Custom lists,... and literally counts and calculates the size of not only the actual file, but attachments or items in the catalogs like WebTemplateCatalog, WebPartCatalog or ListTemplateCatalog. Unfortunately SharePoint 2003 does not provide a direct access to the actual files in attachments through SPAttachmentCollection and instead it only returns the URL of the attachments, so I had to use my own trick to loop in each indivitual attachments as described below:

    1  foreach(SPListItem listitem in list.Items )

    2  {

    3     SPAttachmentCollection attachmentCol = listitem.Attachments;

    4     totalFiles+= attachmentCol.Count;

    5     for(int p=0;p < attachmentCol.Count ; p++)

    6     {

    7         fileName = attachmentCol[p];

    8         fileUrl  = attachmentCol.UrlPrefix + fileName;

    9         SPFile attachFile = web.GetFile(fileUrl);

    10         toalSize += attachFile.Length;

    11     }

    12  }

 

If you run the code above in a context of a web part or a web part page , you will soon get trapped with "Object reference not set to an instance of an object". why? Some lists like survey does not have attachments at all and the code crashes on 3rd line.

To solve this problem, you should treat each list (based on its BaseType) differently than the rest. Here is the complete method to get the count and size of all files of a site.It has worked fine so far:


private void ReportInvidualWeb(SPWeb web)
  {
   string fileName="",fileUrl="";
   int totalFiles=0, toalSize=0;
   try
   {
  SPListCollection listcol = web.Lists;
  foreach(SPList list in listcol )
  {
   switch (list.BaseType )
   {
   case SPBaseType.DocumentLibrary:
    foreach(SPListItem listitem in list.Items )
    {  

     totalFiles++;      
     toalSize += listitem.File.Length;
    }
    break;

   case SPBaseType.DiscussionBoard:
   case SPBaseType.Issue:
   case SPBaseType.GenericList:
       
    if(list.EnableAttachments)
    {
     foreach(SPListItem listitem in list.Items )
     {    
      SPAttachmentCollection attachmentCol = listitem.Attachments;
      totalFiles+= attachmentCol.Count;
      for(int p=0;p < attachmentCol.Count ; p++)
      {
       fileName = attachmentCol[p];
       fileUrl  = attachmentCol.UrlPrefix + fileName;
       SPFile attachFile = web.GetFile(fileUrl);
       toalSize += attachFile.Length;
      }
     }
    }
    break;

   case SPBaseType.Survey:
   case SPBaseType.UnspecifiedBaseType:
   default:
    //Take the appropriate action
    break;     

   }
  }

   }
   catch(Exception e)
   {
  throw e;
   }
   finally
   {
  //save memory, close the web               
  web.Close();               
   }
  }

posted Monday, October 23, 2006 9:11 PM by admin with 0 Comments

Powered by Community Server, by Telligent Systems