Guys im using the spquery object to retrieve some items from a list and populate a repeater. I need to grab the url of the attachment. Ive set IncludeAttachmentUrls to true but im not sure on the name of the field to use in the repeater.

     SPWeb Web = SPContext.Current.Web;
     SPList List = Web.Lists["LinksList"];

     SPQuery Query = new SPQuery();
     Query.Query = "<OrderBy><FieldRef Name='Title' Ascending='False' /></OrderBy>";
     Query.IncludeAttachmentUrls = true;

     RPTLinks.DataSource = List.GetItems(Query).GetDataTable();

2 Answers 2



Since Attachments can be more than one, there is a property in SPListItem.Attachments to access all the attachments, here is how you can access the complete URL:

AttachUrl = properties.ListItem.Attachments.UrlPrefix + properties.ListItem.Attachments[0];

So you need to work on Repeater's ItemDataBound event to bind URL to Hyperlink!


As described by Arsalan, writing your custom code in ItemDataBound can be a perfect choice but in that case you may need to fetch the attachment data again with the item id as you are binding the datatable, and if i am not much mistaken at the time of databind the splistitem information is lost, can hit the performance a little (very small) otherwise you can include the information before you bind the datatable, this is what i do -

SPListItemCollection coll = list.GetItems(oQuery);
DataTable collData = coll.GetDataTable();
collData.Columns.Add("AttachmentNames", typeof(string));
System.Text.StringBuilder attachments=new System.Text.StringBuilder();
for (int i = 0; i < coll.Count; i++)
    if (coll[i].Attachments.Count > 0)
       foreach (string attachment in coll[i].Attachments)
         attachments.Append(attachment).Append("; ");
         //to get the url also
         //attachments.Append(coll[i].Attachments.UrlPrefix).Append(attachment).Append("; ");
       collData.Rows[i]["AttachmentNames"] = attachments.ToString();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.