Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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.

share|improve this question

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?

share|improve this answer
    
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 '11 at 22:23

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.