I have two libraries, one with forms(xml) and one with a custom document content type.

I have an event receiver ItemUpdated on the form library, which interprets the added/modified form, and creates a Document based on this data, by using the DocumentFormat.OpenXml.WordprocessingDocument sdk.

Sidenote: ItemUpdated fires twice (by design it seems). I can not for the life of me get it to fire one time only, if someone could help me with this too, that would be great.

After the package is created, I write it to a MemoryStream, and uses the ToArray() method to create a byte[] array.

Finally, I add the file/byte[] to my library.

Now all this works, and the file is, in fact, added to the library, but I get an exception the second time it runs on this line:

list.RootFolder.Files.Add("filename.docx", bytes, properties, true);

Microsoft.SharePoint.SPException was caught Message=Cannot open file "http://path/filename.docx".

My (stripped) code looks like this:

var generator = new WordDocumentGenerator();
var bytes = generator.CreatePackage();
list.RootFolder.Files.Add("fileName.docx", bytes, properties, true);

The CreatePackage method looks like this:

public byte[] CreatePackage()
    byte[] bytes = null;
    using (MemoryStream stream = new MemoryStream())
    using (var package = WordprocessingDocument.Create(stream, WordprocessingDocumentType.Document))
        CreateParts(package); //Adds parts to the document, no IO
    bytes = stream.ToArray();
return bytes;

Since it works, I'm contemplating just ignoring this error, but naturally that's not really what I want to do.

1 Answer 1


So it appears solving the problem with the double ItemUpdated call will solve the main issue as well. Do you have any other events running against that list item (ItemAdded can be causing this too for example)? If there are other event receivers running on the same item are you using EventFiringEnabled property? Secondly how are you attaching the event handler to the forms library?

  • No other event receivers attached. I use EvenFiringEnabled = false anyways. I attach it through the FatureActivated method, adding the ItemUpdated event to the list programmatically.
    – tarjeieo
    Mar 21, 2011 at 22:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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