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 want to retrieve attachments from a sharepoint list. But don't really know how to, any suggestions?

SOLVED: Check code under picture.

    public void MyData(string prod)
    {
        SPWeb web = SPContext.Current.Web;
        SPList myList = web.Lists.TryGetList("Extern Products");

        if (myList!=null)
        {
            SPQuery query = new SPQuery();
            query.Query = string.Format(
                "<Where><BeginsWith><FieldRef Name='ProductNumber' />" +
                "<Value Type='Text'>{0}</Value></BeginsWith></Where>", prod);
            query.RowLimit = 10;

            SPListItemCollection items = myList.GetItems(query);


            foreach (SPListItem listItem in items)
            {
                TextBox_NameList.Text = listItem["Title"] != null ? listItem["Title"].ToString() : string.Empty;
                TextBox_ListPriceList.Text = listItem["ListPrice"] != null ? listItem["ListPrice"].ToString() : string.Empty;
                TextBox_ProdNumList.Text = listItem["ProductNumber"] != null ? listItem["ProductNumber"].ToString() : string.Empty;
                TextBox_ColorList.Text = listItem["Color"] != null ? listItem["Color"].ToString() : string.Empty;
                TextBox_MoreInformationList.Text = listItem["MoreInformation"] != null ? listItem["MoreInformation"].ToString() : string.Empty;
                //FileUpload_PicList.PostedFile.ContentType = listItem["Attachments"] != null ? listItem["Attachment"].ToString() : string.Empty;
                HiddenField_ID.Value = listItem["ID"].ToString();
            }
        }
    }

    protected void Button_Edit_Click(object sender, EventArgs e)
    {
        if(TextBox_Search.Text == string.Empty)
        {
            Label_ExtensionList.Text = "Who haven't selected anything!";
        }
        else if (ListBox_List.SelectedItem != null)
        {
            MyData = ListBox_List.SelectedItem.Value;
        }
    }

enter image description here

            foreach (SPListItem listItem in items)
            {
                HiddenField_ID.Value = listItem["ID"].ToString(); 
                TextBox_NameList.Text = listItem["Title"] != null ? listItem["Title"].ToString() : string.Empty;
                TextBox_ListPriceList.Text = listItem["ListPrice"] != null ? listItem["ListPrice"].ToString() : string.Empty;
                TextBox_ProdNumList.Text = listItem["ProductNumber"] != null ? listItem["ProductNumber"].ToString() : string.Empty;
                TextBox_ColorList.Text = listItem["Color"] != null ? listItem["Color"].ToString() : string.Empty;
                TextBox_MoreInformationList.Text = listItem["MoreInformation"] != null ? listItem["MoreInformation"].ToString() : string.Empty;

                **foreach (string fileName in listItem.Attachments)
                {
                    SPFile file = listItem.ParentList.ParentWeb.GetFile(
                    listItem.Attachments.Count + fileName);
                    ListBox_Attachment.Items.Add(file.ToString());
                }**
            }
share|improve this question
    
Please add what you did to solve your issue as an answer and when you are able select it as the answer. This improves findability in the search results. –  Robert Kaucher Jun 28 '13 at 1:20

2 Answers 2

We are using an listbox and an button like u told us

       SPWeb web = SPContext.Current.Web;
        SPList myList = web.Lists["MyList"];

        SPListItem item = myList.GetItemById(Convert.ToInt32(HiddenField1.Value));
        string listUrl = web.Url + myList.RootFolder.Url;

        string attachmentUrl;
        if (item.Attachments.Count > 0)
            //items are red
        {

            string filename = item.Attachments[0];
            attachmentUrl = listUrl + "/attachments/" + item.ID + "/" + filename;
            //attachmenturl is never used , listurl are red 
        }

EDIT: Updated the code

share|improve this answer
    
I don't get it. is this an answer or a comment to Arsalan answer? –  SPArchaeologist Apr 11 '13 at 14:04

Kristian,

Instead of showing it on Upload control, take a ListBox or GridView with Delete Button(X).. You will need to bind the Listbox/GridView with Attachments for the List Item... You can access the attachments from SPListItem.Attachments property! You will see that ListItem.Attachments will give you a string collection, if you want to get the URL of the attachment, you can do following:

First you need to get the url for the list itself. The url for the list is simply:

string listUrl = web.Url + "/" + list.RootFolder.Url;

Then, you use the list url, the item id and the attachment name to construct the whole attachment url. The completed url is:

string attachmentUrl;
if (listItem.Attachments.Count > 0)
{
    string filename = listItem.Attachments[0];
    attachmentUrl = listUrl + "/attachments/" + item.ID + "/" + filename;
}

For deleting a specific Attachment you can use SPListItem.Attachments.Delete() method... You can do something like:

listItem.Attachments.Delete(fileName);

Let me know if you stuck!

share|improve this answer
    
On the delete button, for deleting the attachments! And you will be giving File Upload control for more attachments if user wants more than one attachments! –  Arsalan Adam Khatri Dec 5 '12 at 13:27
    
Do you know how to bind a GridView and take a custom button on GridView? These are quite ASP.NET basics... –  Arsalan Adam Khatri Dec 5 '12 at 13:41
    
Kristian, I recommend you to view ASP.NET tutorials first... How to bind gridview, adding custom command buttons, how to pass arguments to command buttons etc! Google is your friend.. :) –  Arsalan Adam Khatri Dec 5 '12 at 13:55
    
Yes, but with GridView you can do a lot more! –  Arsalan Adam Khatri Dec 5 '12 at 14:00
    
Kristian, as you have bind the SPListItems in ListBox, bind the attachments to another ListBox in Update Web Part... And give a Delete button next to it, as you have given Edit Button! Now on delete, get the selected Item from ListBox and use delete Attachment code! –  Arsalan Adam Khatri Dec 5 '12 at 14:05

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.