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

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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 – Batistuta 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

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.