Thursday, 29 October 2009

I'm in love...with the new OpenXML SDK!

Tobias Lekman, Technical Architect

I have spent the last couple of weeks creating a Word report based on SharePoint data that spans over a hundred pages using tables and custom graphs.

To do this, I used OpenXML. The file format (.docx) is basically a ZIP file containing a bunch of XML files. I did this using LINQ, XSLT and System.IO.Packaging to compress and order the XML sections into a .docx file. The OpenXML SDK version 1 allows you to manage the packaging side and relationships of the “parts” (XML files).

The new SDK allows you to manage the entire document directly using LINQ which would have bypassed my 60+ XSLT library that I had to create for the original conversion to work. The structure of the program would have been the same but could have saved me a lot of code lines!

When creating OpenXML documents you often end up with corruption. The file is missing a relationship between a diagram and the underlying data, an ID of a part is repeated and so forth. Opening the file in Word will give you a nasty error without any detailed information and you are left to open the document and manually figure out where the corruption could have occurred. The SDK now contains a class called OpenXmlValidator which will validate the entire package and give you a detailed error report. Again, this could have saved me hours and hours of debug time.

To make things even better, the SDK contains some fantastic tools. My favourite is the Document Reflector which opens an OpenXML document from a template and will give you the full code for how to generate that document using the SDK. Now, I don’t even want to begin to estimate how much time THAT would have saved me.

As a final nail in my coffin, SharePoint 2010 introduces REST for Excel Services. My report contained 50+ graph reports which had to be created using 6 different XML parts. This can now be done by inserting the graph generated directly by Excel Services using SharePoint 2010.

The final version of the new SDK will be released together with Office 2010.

0 comments:

Post a Comment