Posted by: alirezaa79 | May 2, 2011

How to get size of Sharepoint 2010 list item attachment?

Users can upload one attachement per item in Sharepoint list. If sharepoint list has many items and attachment get this attachments and its size is very difficult (maye impossible).

developers can get attachments and its size from list with sharepoint object model.

By below code and load in sharepoint page (user controls), developer can do that.

1- add asp:label in design mode.

<asp:Label runat=”server” ID=”lbl01″ />

2- write this code in code behind

protected void Page_Load(object sender, EventArgs e)
SPSite site = new SPSite(“[Sharepoint Server URL ]”);
SPWeb web = site.OpenWeb();
SPList list = web.Lists[“List Name”];lbl01.Text = getAttachmentSize(list);

public String getAttachmentSize(SPList list)
SPListItemCollection items = list.Items;
String strSize = “”;
foreach (SPListItem item in items)

SPFolder sourceItemAttachmentsFolder = item.Web.Folders[“List Name”].SubFolders[item.ParentList.Title].SubFolders[“Attachments”].SubFolders[item.ID.ToString()];
foreach (SPFile file in sourceItemAttachmentsFolder.Files)
strSize = strSize + file.Length.ToString() + “<br />”;

return strSize;

This code can run sharepoint 2007 environment.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s


%d bloggers like this: