9

How to get attachment full URL programmatically From SharePoint list item?

Explanation:

I have adding new item with attachment. Now I have to bind that attachment link to grid. When click on that link Attachment should be open.

How can I get Attachment URL of that item?

3 Answers 3

10

I found answer for my question.

private string GetAttachmentUrls(SPListItem oItem)
{

        string path = string.Empty;

        try

        {

            path = (from string file in oItem.Attachments
                              orderby file
                              select SPUrlUtility.CombineUrl(oItem.Attachments.UrlPrefix, file)).FirstOrDefault();
            return path;
        }
        catch
        {
            return string.Empty;
        }

    }
1
  • can you please give a bit explanation of this code?
    – Madhav
    Commented Jun 16, 2016 at 12:22
0

I did it in this way:

    private string GetAttachmentURL(SPAttachmentCollection attchmentcollection) {
        string URL="#";
        foreach (var item in attchmentcollection)
        {
            URL = attchmentcollection.UrlPrefix + item;
            break;
        }

        return URL;
    }
-2

Try using below code:

string attachmentURL = splistitem.Attachments.UrlPrefix + splistitem.Attachments[0];

Regards,

Nabeel

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.