-1

i want to add attachment file Sharepoint 2013 library but it doesnt work.

here is my code

protected void Button1_Click(object sender, EventArgs e)
    {
        using (SPSite oSPsite = new SPSite("http//"))
        {
            using (SPWeb oSPWeb = oSPsite.OpenWeb())
            {
                oSPWeb.AllowUnsafeUpdates = true;
                // Fetch the List
                SPList list = oSPWeb.Lists["ornekdosya"];
                // Get the List item
                SPListItem listItem = list.GetItemById(1);

                // Get the Attachment collection
                SPAttachmentCollection attachmentCollection = listItem.Attachments;
                Stream attachmentStream;
                Byte[] attachmentContent;
                // Get the file from the file upload control
                if (this.FileUpload1.HasFile)
                {
                    attachmentStream = this.FileUpload1.PostedFile.InputStream;

                    attachmentContent = new Byte[attachmentStream.Length];
                    attachmentStream.Read(attachmentContent, 0, (int)attachmentStream.Length);
                    attachmentStream.Close();
                    attachmentStream.Dispose();
                    // Add the file to the attachment collection
                    attachmentCollection.Add(this.FileUpload1.FileName, attachmentContent);
                }
                // Update th list item
                listItem.Update();
                oSPWeb.AllowUnsafeUpdates = false;
            }
        }

here is my sharepoint list

enter image description here

Any help is appreciated! Thanks!

  • 1
    Are you attaching file or uploading file into the document library? attachment and upload or different thing? – Arulraj Sep 3 '18 at 12:14
  • attaching file to document library. – mert Sep 3 '18 at 12:16
  • 2
    We can't add an attachment for document library item. – Arulraj Sep 3 '18 at 12:33
1

Above code is used to attach a document to List Item not for Document Library .Please use below code to add to document Library.

    protected void Button1_Click(object sender, EventArgs e)
        {
            using (SPSite oSPsite = new SPSite("http//"))
            {
                using (SPWeb oSPWeb = oSPsite.OpenWeb())
                {
                    oSPWeb.AllowUnsafeUpdates = true;

                    Stream attachmentStream;
                    Byte[] attachmentContent;
                    // Get the file from the file upload control
                    if (this.FileUpload1.HasFile)
                    {
                        attachmentStream = this.FileUpload1.PostedFile.InputStream;

                        attachmentContent = new Byte[attachmentStream.Length];
                        attachmentStream.Read(attachmentContent, 0, (int)attachmentStream.Length);

                          SPFolder myLibrary = oWeb.Folders["Shared Documents"];

        string fileName=this.FileUpload1.FileName.ToString();
                 Boolean replaceExistingFiles = true;
 SPFile spfile = myLibrary.Files.Add(fileName, attachmentStream , replaceExistingFiles);

        // Commit 
        myLibrary.Update();
         attachmentStream.Close();
           attachmentStream.Dispose();
                    }

                    oSPWeb.AllowUnsafeUpdates = false;
                }
            }
  • SPFile spfile = myLibrary.Files.Add(fileName, attachmentStream , replaceExistingFiles); filename does not exist. why ? – mert Sep 4 '18 at 5:03
  • I think you should use string fileName=this.FileUpload1.FileName.ToString(); instead of string fileName=fileupload.FileName.ToString(); i will update the answer also – vinit kumar Sep 4 '18 at 5:08
  • its work ! ! i success to add but i cant open my file. any help ? i click on my file but doesnt seen anything, nothing. thanks. – mert Sep 4 '18 at 5:20
  • i updated the answer , may be stream is closed and disposed before File Addition so move attachmentStream.Close(); attachmentStream.Dispose() after myLibrary.Update();. Then give a try – vinit kumar Sep 4 '18 at 5:23
  • I am thankful to you bro ! ! ! ! its work !!!! thanks for helps.. i change dispose and close argument and it work! ! ! ! ! thanks – mert Sep 4 '18 at 5:35
0

after this arrangament my code is here sadly it doesnt work :(

if (this.FileUpload1.HasFile)
                {
                    attachmentStream = this.FileUpload1.PostedFile.InputStream;

                    attachmentContent = new Byte[attachmentStream.Length];
                    attachmentStream.Read(attachmentContent, 0, (int)attachmentStream.Length);
                    attachmentStream.Close();
                    attachmentStream.Dispose();
                    SPFolder myLibrary = oSPWeb.Folders["ornekdosya"];
                    myLibrary.Update();      
                    string fileName;
                    fileName = this.FileUpload1.FileName.ToString();
                    Boolean replaceExistingFiles = true;
                    SPFile spfile = myLibrary.Files.Add(fileName, attachmentStream, replaceExistingFiles);
                    // Commit    
                }

                oSPWeb.AllowUnsafeUpdates = false;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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