Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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();
     RPTLinks.DataBind();
share|improve this question

2 Answers 2

up vote 2 down vote accepted

Richards,

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!

share|improve this answer

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)
    {
       attachments.Clear();
       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();
    }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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