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.
2 Answers
private IEnumerable<string> GetAttachmentUrls(SPListItem item) { return from string fileName in item.Attachments orderby fileName select SPUrlUtility.CombineUrl(item.Attachments.UrlPrefix, fileName); } - 5Always use SPUrlUtility.CombineUrl to combine urlsPer Jakobsen– Per Jakobsen2012-03-20 20:03:51 +00:00Commented Mar 20, 2012 at 20:03
- Doesn't this return an
IEnumerable<string>, though. I thinkstring[]as the return type might not work...Robert Kaucher– Robert Kaucher2012-12-07 17:21:09 +00:00Commented Dec 7, 2012 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– skeletank2012-12-07 17:30:38 +00:00Commented Dec 7, 2012 at 17:30
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; } } } }