Take the 2-minute tour ×
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);
         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 ....
share|improve this question
add comment

1 Answer

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

Your Answer

 
discard

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.