none
Programatically adding a Word attachment to a list causes the Word document to have unreadable content

    Question

  • I have a Sharepoint custom list. If I use the "Add Item" feature and add an item with a Word document attachment, it all works fine and I am able to open the document in Read-only mode and to change it to Edit mode.

    If I programmatically add an item to the list with the exact same attachment via a Visual Web Part button click event created in Visual Studio (I am creating the new item in the list, and adding an attachment at the same time), the item is added to the list with the attachment and it looks okay, but when I try to open the Word attachment, I get "The file is corrupt and can't be opened....Word found unreadable content..." error message. If I say I want to recover the contents, I can view the document (opened in read-only mode), but if I click to Edit the document, it says "A problem occurred while connecting to the server..."  Again the very same document works fine (opening, reading and editing) if I attach it via the "Add Item" on the list. 

    To add the item with the Word attachment programatically, I am using:

    Using web As SPWeb = mySPSite.OpenWeb()

                web.AllowUnsafeUpdates = True

                web.ParserEnabled = False -----I tried putting in this line from another article I had read but it doesn't help

     

                Dim list As SPList = Utility.TryGetList(web, "TestList", False)

                Dim listItems As SPListItemCollection = list.Items 

                Dim newItem As SPListItem = list.AddItem()

                newItem("Title") = myADUser.Name

                Dim fs As FileStream = File.OpenRead(FileName)

                Dim contents(CInt(fs.Length)) As Byte

                fs.Read(contents, 0, CInt(fs.Length)) 

                fs.Close() 

                newItem.Attachments.Add("TestDocument.doc", contents) 

                newItem.Update() 

            End Using

    I tried 3 different documents, all with the same result.  If I manually add an item to the list and attach the Word document, everything works fine. If I programatically add an item with a Word attachment, the documents get attached but are deemed corrupt when I try to open them, regardless of the Word document I am using.

    I'm guessing there's something wrong with the way I'm doing file.openread maybe?  But I'm not sure what as this is my first foray into filestreams.  It kind of works as the document gets there, it just doesn't get there uncorrupted. 

    Thank you so much for any thoughts/help you can offer.

    Thursday, January 19, 2012 1:52 PM

Answers

  • Update - I actually got this working by using a memory stream to create a byte array, and using the OpenXML tool to get the code behind for the Word document, to create the document itself.  I'm posting here to possibly prevent someone else from going insane.

    Using myMemStream As MemoryStream = New MemoryStream

                    Using package As WordprocessingDocument = WordprocessingDocument.Create(myMemStream, WordprocessingDocumentType.Document)

                        CreateParts(package)

                    End Using 

     

                    Using web As SPWeb = mySPSite.OpenWeb()

                        Dim list As SPList = Utility.TryGetList(web, "TestList", False)

                        Dim listItems As SPListItemCollection = list.Items 

                        Dim newItem As SPListItem = list.AddItem()

                        newItem("Title") = myADUser.Name

    newItem.Attachments.Add("TestDocumentMemStream.doc", myMemStream.ToArray()) 

                        newItem.Update()

                    End Using

     End Using

     

    • Marked as answer by KimFromPA Thursday, January 19, 2012 4:12 PM
    Thursday, January 19, 2012 4:12 PM