locked
how to programmatically get attachments from a custom list? RRS feed

  • Question

  • Please tell me how can i get attachments from a custom list in MOSS 2007

    P.S. I want to do this by program not manually
    • Edited by Mike Walsh FIN Wednesday, March 30, 2011 7:05 AM Title re-written. Was "MOSS 2007 related question" which is a poor problem description. P.S. taken from "reply" from same poster that was then deleted.
    • Moved by Mike Walsh FIN Wednesday, March 30, 2011 7:06 AM "programmatically" so Programming forum not general (From:SharePoint - General Question and Answers and Discussion (pre-SharePoint 2010))
    Wednesday, March 30, 2011 6:40 AM

Answers

  • Hi,

    I have already given you code sample in your duplicate thread.

    Try below code:

    SPSite objSite = new SPSite("http://Testsite/");

    using (SPWeb objWeb = objSite.OpenWeb())
    {
    objWeb.AllowUnsafeUpdates = true;

    // Get the List
    SPList objList = objWeb.Lists["MyList"];

    // Get the item by ID
    SPListItem objItem = objList.GetItemById(1);

    // Get the attachments of the item
    SPAttachmentCollection objAttchments = objItem.Attachments;

    Also you can refer below link for different code:

    http://www.dotnetking.com/TechnicalComments.aspx?LogID=352

    Hope it could help

    Here is the link of duplicate:

    http://social.msdn.microsoft.com/Forums/en/sharepointdevelopment/thread/630e14fd-690d-4475-ad15-ee7d41dada8a


    Cheers, Hemendra-MCTS "Yesterday is just a memory,Tomorrow we may never see"
    • Marked as answer by Emir Liu Thursday, April 7, 2011 5:16 AM
    Wednesday, March 30, 2011 7:35 AM
    Moderator
  • Hi Tanvi,

     

    To access the attachments use the below code

    // Get the item
    SPListItem objItem = objList.GetItemById(12);

    // Get the attachments of the item
    SPAttachmentCollection objAttchments = objItem.Attachments;

     

    To download the attachment to a location use,

    SPWeb web = new SPSite("<Site URL>").OpenWeb();
    //Open List
    SPList list = web.Lists["<ListName>"];
    //Get the item
    SPListItem item = list.Items[1];
    //Get the folder
    SPFolder folder = web.Folders["Lists"].SubFolders[strListName].SubFolders["Attachments"].SubFolders[item.ID.ToString()];

    foreach(SPFile file in folder.Files)
    {
    byte[] binFile = file.OpenBinary();
    System.IO.FileStream fstream = System.IO.File.Create("c:\\MyDownloadFolder\\" + file.Name);
    fstream.Write(binFile, 0, binFile.Length);
    }

     

    Thanks,
    Chanakya

    • Marked as answer by Emir Liu Thursday, April 7, 2011 5:16 AM
    Wednesday, March 30, 2011 7:38 AM

All replies

  • Hi,

    I have already given you code sample in your duplicate thread.

    Try below code:

    SPSite objSite = new SPSite("http://Testsite/");

    using (SPWeb objWeb = objSite.OpenWeb())
    {
    objWeb.AllowUnsafeUpdates = true;

    // Get the List
    SPList objList = objWeb.Lists["MyList"];

    // Get the item by ID
    SPListItem objItem = objList.GetItemById(1);

    // Get the attachments of the item
    SPAttachmentCollection objAttchments = objItem.Attachments;

    Also you can refer below link for different code:

    http://www.dotnetking.com/TechnicalComments.aspx?LogID=352

    Hope it could help

    Here is the link of duplicate:

    http://social.msdn.microsoft.com/Forums/en/sharepointdevelopment/thread/630e14fd-690d-4475-ad15-ee7d41dada8a


    Cheers, Hemendra-MCTS "Yesterday is just a memory,Tomorrow we may never see"
    • Marked as answer by Emir Liu Thursday, April 7, 2011 5:16 AM
    Wednesday, March 30, 2011 7:35 AM
    Moderator
  • Hi Tanvi,

     

    To access the attachments use the below code

    // Get the item
    SPListItem objItem = objList.GetItemById(12);

    // Get the attachments of the item
    SPAttachmentCollection objAttchments = objItem.Attachments;

     

    To download the attachment to a location use,

    SPWeb web = new SPSite("<Site URL>").OpenWeb();
    //Open List
    SPList list = web.Lists["<ListName>"];
    //Get the item
    SPListItem item = list.Items[1];
    //Get the folder
    SPFolder folder = web.Folders["Lists"].SubFolders[strListName].SubFolders["Attachments"].SubFolders[item.ID.ToString()];

    foreach(SPFile file in folder.Files)
    {
    byte[] binFile = file.OpenBinary();
    System.IO.FileStream fstream = System.IO.File.Create("c:\\MyDownloadFolder\\" + file.Name);
    fstream.Write(binFile, 0, binFile.Length);
    }

     

    Thanks,
    Chanakya

    • Marked as answer by Emir Liu Thursday, April 7, 2011 5:16 AM
    Wednesday, March 30, 2011 7:38 AM
  • > I have already given you code sample in your duplicate thread.

    Tanvi_Sharma:

    NEVER post the same question twice. It causes (as you can see here) duplicate threads and wastes the time of people like Hemendra who are trying to help you.

    In addition your parallel thread had an extremely poor Title "please reply here" which given absolutely no indication of the question. It also asked people to reply to your e-mail address. This is incorrect for forum posts. Replies to forum posts should go to the forums so that other people can also benefit from them.

    I have deleted that duplicate thread.

    ------------------------------------

    Hemendra: Thanks for taking the extra effort to post your answer twice. I hope the OP here will in future not make this necessary.

     

    Moderator


    SP 2010 "FAQ" (mainly useful links): http://wssv4faq.mindsharp.com/default.aspx
    WSS3/MOSS FAQ (FAQ and Links) http://wssv3faq.mindsharp.com/default.aspx
    Both also have links to extensive book lists and to (free) on-line chapters
    Wednesday, March 30, 2011 8:51 AM
  • tHANKS FOR YOUR VERY KIND SUGGESTION.MR mike
    tanvi sharma
    Thursday, April 7, 2011 5:24 AM
  • yes i have solved this problem thanks for your reply
    tanvi sharma
    Thursday, April 7, 2011 5:24 AM
  • yes i have solved this problem thanks for your reply
    tanvi sharma
    Thursday, April 7, 2011 5:25 AM
  • YOU have already told me that Title of my Question was poor in that thread i already know this  ok
    tanvi sharma
    Thursday, April 7, 2011 5:26 AM