Retrieving an attachment from an item using code

Posted: October 20, 2008 in Attachments, Object Model

foreach (SPListItem attachItem in list.Items)
{
if (Convert.ToString(attachItem[“Request Number”]) == Convert.ToString(this.Page.Request.QueryString[“reqno”]))
{
foreach (string fileName in attachItem.Attachments)
{
SPFile file = attachItem.ParentList.ParentWeb.GetFile(attachItem.Attachments.UrlPrefix + fileName);
writer.RenderBeginTag(HtmlTextWriterTag.Tr);
writer.AddAttribute(HtmlTextWriterAttribute.Target, “_blank”);
writer.AddAttribute(HtmlTextWriterAttribute.Href, file.ServerRelativeUrl);
writer.AddStyleAttribute(HtmlTextWriterStyle.FontSize, “12px”);
writer.RenderBeginTag(HtmlTextWriterTag.A);
writer.Write(file.Name);
writer.RenderEndTag();
flag = true;
}
break;
}
}

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s