locked
For custom user control form need to write code for Multiple file attchment RRS feed

  • Question

  • HI ,

    I have created a custom form for my portal in which i want to attch multiple attchments......

    Please help me in writing code..


    sudhanshu sharma Do good and cast it into river :)

    Tuesday, April 10, 2012 1:29 PM

Answers

  • Hi sudhanshuSharma

    You can write a custom multiple attachments form just like writing a Asp.Net multiple file upload form. The only difference between them is that the files are stored in items of SharePoint list as attachments.

    You can store file in an item as an attachment by writing following code:

                                     

                          SPList list = Web.Lists["ListName"];//get your list here
                            SPListItem Item = list.Items.Add();
                            FileStream file = File.OpenRead(FilePath)
                            byte[] bytes = new byte[file.Length];
                            file.Read(bytes, 0, bytes.Length);
                            SPAttachmentCollection attachments = Item.Attachments;
                            //add file to item
                            attachments.Add(AttachmentName, bytes);
                            Item.SystemUpdate(false);
                            Item.Update();
                            file.Close();

    There are also many article about the multiple file upload such as this article. Hope that helps.

    Thanks,


    Lambda Zhao

    TechNet Community Support

    Thursday, April 12, 2012 9:59 AM