views:

460

answers:

2

Hi all,

I've been frustrated by this for the entire weekend, plus a day or two, so any help would be significantly appreciated.

I'm trying to write a program that can programmatically go into a SharePoint 2007 doc library, open a file, change the contents of the file, then put the file back. I've gotten all but the last part of this down. The reason Office Open XML is involved is that that's how I'm opening the document and modifying it - through the Office Open XML SDK. My question is: How do I get it from the document back into the library?

The problem as I see it is that there's no save function on the WordprocessingDocument object itself. This prevents me from saving it into the SPFile's SaveBinary function.

A: 

Yesterday I saw a webcast with Andrew Connell where he opened a doc from a doc library, added a watermark and saved the file again. It sure sounds like you should have a look at that webcast: https://msevents.microsoft.com/CUI/WebCastRegistrationConfirmation.aspx?culture=en-US&RegistrationID=1299758384&Validate=false

btw I found that all 10 of the web casts in that serie were very good.

Kasper
I tried doing what he did as closely as possible. However, he did not show the piece of code to put the item back into SharePoint! Really quite annoying (not that I'm blaming you. Annoying of HIM).
+2  A: 

You should use stream's to write back the changed OOXML into the SPFile. I hope this example helps!

Stream fs = mySPFile.OpenBinaryStream();

using (WordprocessingDocument ooxmlDoc = WordprocessingDocument.Open(fs, true))
{

    MainDocumentPart mainPart = wordDoc.MainDocumentPart;
    XmlDocument xmlMainDocument = new XmlDocument();
    xmlMainDocument.Load(mainPart.GetStream());

   // change the contents of the ooxmlDoc / xmlMainDocument

   Stream stream = mainPart.GetStream(FileMode.Open, FileAccess.ReadWrite);
   xmlMainDocument.Save(stream);
   // the stream should not be longer than the DocumentPart
   stream.SetLength(stream.Position); 
}
mySPFile.SaveBinary(fs);
fs.Dispose();