Programatically Upload Multiple File in SharePoint OnPrem List (Part 2)

Hello All,

This is second part of the series of "Programatically Upload Multiple File in SharePoint OnPrem 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.

1. 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
http://cid-9f89234e32135c71.office.live.com/self.aspx/.Public/SP%5E_NewFormwithAttachment.zip

1. Upload the files in local temp directory.

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

//-----------------------------------------------------------
HttpFileCollection hfc = Request.Files;
        for (int i = 0; i < hfc.Count; i++)
        {
            HttpPostedFile hpf = hfc[i];
            if (dtAttachFile == null)
            {
                AddMoreColumns();
            }
            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;
                hpf.SaveAs(_filepath);       
                drAttachFile["FileName"] = _fileName;
                drAttachFile["FilePath"] = _filepath;
                dtAttachFile.Rows.Add(drAttachFile);   
            }
        }

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);
            fStream.Close();
            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,

References:
http://www.c-sharpcorner.com/UploadFile/sarav82/MOSS11072007065009AM/MOSS.aspx

Jayant Sharma