Wednesday, 11 July 2012

How to get attached file url in sharepoint using SPAttachmentsCollection

Hi Friends,

I was searching one solution that how can i have url of Attached item, but mostly i got hard coding

mostly searched solution is like this

"/you Stie/List/Your list name/Attachments/" + item("ID").ToString() + "/" + obj.ToString

and this is really bad to use.
But now i have explored Sharepoint and get solution, and its very simpl.
Here is the solution.

 dim _url as string= item.Attachments.UrlPrefix.ToString + item.Attachments.Item(0).ToString

Explations:
1)
item.item.Attachments.UrlPrefix
it will return till id value in the form of url

2)
 item.Attachments.Item(0).ToString


This will return the item name that we need

by this solution we will have our url of Attached item

Another Solution by linq

private string[] GetAttachmentUrls(SPListItem item)
{
  return from string fileName in repeaterItem.Attachments
         orderby fileName
         select SPUrlUtility.CombineUrl(item.Attachments.UrlPrefix, fileName);
}

No comments:

Post a Comment