Hello All,

This is second part of the series of "Programatically Upload Multiple File in SharePoint List"

Go To PART 1
Go To PART 2

In Part I of this series, I had show you some method through which we can design our custom SharePoint Page with Attachment functionality, so that Its looks and feel should be similar to existing SharePoint NewForm.aspx window.
Now it is time to show how we can upload multiple files.

Uploading of multiple file can be done in three steps.

Upload the files in local temp directory.
2. Upload the files from temp directory to SharePoint List
3. Delete the files from temp directory.

you can download the source code from

1. Upload the files in local temp directory.

   private void AddMoreColumns()
       dtAttachFile = new DataTable("Files");
       dcAttachFile = new DataColumn("FileName", Type.GetType("System.String"));
       dcAttachFile = new DataColumn("FilePath", Type.GetType("System.String"));

HttpFileCollection hfc = Request.Files;
        for (int i = 0; i < hfc.Count; i++)
            HttpPostedFile hpf = hfc[i];
            if (dtAttachFile == null)
            if (hpf.ContentLength > 0)
                drAttachFile = dtAttachFile.NewRow();
                string _tempFolder = Environment.GetEnvironmentVariable("TEMP");
                string _fileTime = DateTime.Now.ToFileTime().ToString();
                string _fileName = System.IO.Path.GetFileName(hpf.FileName);
                string _newfilePath = _fileTime + "~" + _fileName;

                string _filepath = _tempFolder + "\\" + _newfilePath;
                drAttachFile["FileName"] = _fileName;
                drAttachFile["FilePath"] = _filepath;

2. Upload the files from temp directory to SharePoint List

        int _dtAttachFilecount = dtAttachFile.Rows.Count;
        SPList myList = this._myTeamSite.Lists["L1"];     //L1 SharePoint List
        SPListItem newItem = myList.Items.Add();
        foreach (DataRow drAttachFile in dtAttachFile.Rows)
            fileName = drAttachFile["Filename"].ToString();    
            strFilepath = drAttachFile["FilePath"].ToString();
            StreamReader sr = new StreamReader(strFilepath);
            Stream fStream = sr.BaseStream;
            contents = new byte[fStream.Length];
            fStream.Read(contents, 0, (int)fStream.Length);
            newItem.Attachments.Add(fileName, contents);    //Attach the files to sharepoint List
            System.IO.File.Delete(strFilepath);                       //3. Delete the files from temp directory.
        this._myTeamSite.AllowUnsafeUpdates = true;
         newItem.Update();                                            //Commit the attachment
        this._myTeamSite.AllowUnsafeUpdates = false;

At first step we need to upload the files in temp directory, but for differentiate all files we can append the file name with timestamp and then upload it to temp directory. once we upload the files from temp to SPList we can remove it from temp directory,


Jayant Sharma