Home > Uncategorized > How to get the size of attachments in list items using SharePoint object model

How to get the size of attachments in list items using SharePoint object model

October 24th, 2006 Leave a comment Go to comments

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();               
   }
  }

Categories: Uncategorized Tags:
  1. No comments yet.