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

I have an SPListItem for which I would like to get the urls of all its attachments. The urls will be used as part of my custom web part which has an SPList of SPListItem items.

share|improve this question

migrated from stackoverflow.com Mar 20 '12 at 19:50

This question came from our site for professional and enthusiast programmers.

add comment

2 Answers 2

up vote 6 down vote accepted
private IEnumerable<string> GetAttachmentUrls(SPListItem item)
{
  return from string fileName in item.Attachments
         orderby fileName
         select SPUrlUtility.CombineUrl(item.Attachments.UrlPrefix, fileName);
}
share|improve this answer
4  
Always use SPUrlUtility.CombineUrl to combine urls –  Per Jakobsen Mar 20 '12 at 20:03
    
Doesn't this return an IEnumerable<string>, though. I think string[] as the return type might not work... –  Robert Kaucher Dec 7 '12 at 17:21
1  
@RobertKaucher You would be correct. I think that I forgot to add in the parentheses and the .ToList() when I originally wrote this example. –  skeletank Dec 7 '12 at 17:30
add comment

I have used the following code in an itemAdded event receiver.

        public override void ItemAdded(SPItemEventProperties properties)
        {
            if (properties.ListTitle == "Architecture Calendar")
            {

                base.ItemAdded(properties);
                string eventUrl = null;
                string AttachUrl=null;

                using (SPSite site = properties.OpenSite())
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        try
                        {
                            SPList listCalendar = web.Lists[properties.ListId];
                            SPItem itemCalendar = properties.ListItem;
                            SPSite mainSite = new SPSite(site.Url + @"/admin/");
                            SPWeb mainWeb = mainSite.OpenWeb();

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

                            SPList listIntegrated = mainWeb.Lists["IntegratedEvents"];
                            SPListItem itemIntegrated = listIntegrated.Items.Add();

                            **itemIntegrated["Picture"] = AttachUrl;**



                            itemIntegrated.Update();


                        }
                        catch (Exception Ex)
                        {
                            throw Ex;
                        }
                    }
                }
            }
share|improve this answer
add comment

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.