Sign up ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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);
         SPAttachmentCollection empDocAttachment = employeeDocListItem.Attachments;
         string empDocFileName = Path.GetFileName(FileUploadDocuments.FileName);

         empDocAttachment.Add(empDocFileName, empDocContents);


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

//.... cut ....
share|improve this question

1 Answer 1

Well just keep calling


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];



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

share|improve this answer
please be more specified i dont get it – Batistuta Apr 11 '13 at 13:34
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. – Robert Lindgren Apr 11 '13 at 13:37

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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