1

This code creates a new listitem for every attachment but i only want to create 1 listitem and add all attachments to it

SPList myEmployeeDocList = web.Lists.TryGetList("Document cv");
if (myEmployeeDocList != null)
{
    SPListItem employeeDocListItem = myEmployeeDocList.Items.Add();
    employeeDocListItem["Title"] = strUserName;

    if (FileUploadDocuments.PostedFile != null && FileUploadDocuments.HasFile)
    {
         Stream fStream = FileUploadDocuments.PostedFile.InputStream;
         byte[] empDocContents = new byte[fStream.Length];
         fStream.Read(empDocContents, 0, (int) fStream.Length);
         fStream.Close();
         fStream.Dispose();
         SPAttachmentCollection empDocAttachment = employeeDocListItem.Attachments;
         string empDocFileName = Path.GetFileName(FileUploadDocuments.FileName);

         empDocAttachment.Add(empDocFileName, empDocContents);
    }

    employeeDocListItem.Update();

    string empDocListUrl = web.Url + "/" + myEmployeeDocList.RootFolder.Url;
    string empDocAttachmentUrl = empDocListUrl + "/attachments/" + employeeDocListItem.ID + "/" +
    FileUploadDocuments.FileName;

//.... cut ....

1 Answer 1

1

Well just keep calling

 employeeDocListItem.Attachments.Add()

for all of your attachments, preferably in a loop

foreach(var attachment in AllMyAttachments)
{
        var fs = new FileStream(attachment.FullName , FileMode.Open,FileAccess.Read);

        byte[] theData = new byte[fs.Length];

        fs.Read(theData,0,System.Convert.ToInt32(fs.Length));

        fs.Close();

        employeeDocListItem.Attachments.Add(attachment.Name, theData);
}

employeeDocListItem.Update()
2
  • please be more specified i dont get it
    – Batistuta
    Commented Apr 11, 2013 at 13:34
  • 1
    I really can't be more specific than this. You need to create a collection containing all of the files you wnat to add as attachments, call that AllMyAttachments and then copy paste the code block above. Commented Apr 11, 2013 at 13:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.