Question

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.

Was it helpful?

Solution

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();

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top