Sunday, June 21, 2009

Reusing the PowerShell PowerTools for Open XML in your C# or VB.Net world

OpenXML Developer.org - Accessing the C# code from PowerTools for Open XML in a .NET application

“PowerTools, at http://www.codeplex.com/PowerTools makes server-side document assembly of Open XML documents very easy in a PowerShell script. It does this by providing a rich set of PowerShell cmdlets. Interestingly, the smarts developed in PowerTools are also applicable to an integrated .NET business application - typically written instead in c# or VB.NET. It is simple to tap into this functionality because the PowerTools download includes source code, and within the source code, the implementation of the cmdlets already sits over a clean set of reusable classes.

This article demonstrates how to tap into this goodness. To prove the approach, an earlier sample written in PowerShell using PowerTools at http://openxmldeveloper.org/archive/2009/04/06/4418.aspx is redeveloped in c# as a windows application, and could also equally have been a web application.

My approach includes a wrapper around the PowerTools base classes. The wrapper provides .NET methods that match the PowerShell cmdlets. My wrapper is not complete in that it mostly has methods matching the cmdlets I wanted to use - but you will see that very little code is required to extend the wrapper to access any functionality in PowerTools. For example, if you wanted to use the logic provided by the cmdlet for setting the footer Set-OpenXmlContentFormat you can add that yourself with just a few lines of glue code.

The sample mentioned above uses a few methods which grabs parts from several documents and merges them in to one document. It builds a report document dynamically based on some current environment properties (processor and disk load).

…”

When I first saw the article I thought it was going to talk about calling PS cmdlets from within the C# code. Nope! Instead if talks about reusing the actual source, modifying it to fit better and directly into your C#/VB.Net application.

You’ve just got to love code reuse! (And open source/source available projects with permissive licenses that allow this kind of re-use…  :)

(via OpenXML Developer.org - New library article: Use PowerTools for Open XML to view C# code in a .NET application)

 

Related Past Post XRef:
PowerShell, OpenXML, WMI and the PowerTools for OpenXML = Doc generation for our inner geek
Because it’s a PowerShell kind of day… PowerTools for Open XML V1.1 Released
OpenXML PowerTools updated – Cell your Excel via PowerShell
Powering into OpenXML with PowerShell

No comments:

Post a Comment

NOTE: Anonymous Commenting has been turned off for a while... The comment spammers are just killing me...

ALL comments are moderated. I will review every comment before it will appear on the blog.

Your comment WILL NOT APPEAR UNTIL I approve it. This may take some hours...

I reserve, and will use, the right to not approve ANY comment for ANY reason. I will not usually, but if it's off topic, spam (or even close to spam-like), inflammatory, mean, etc, etc, well... then...

Please see my comment policy for more information if you are interested.

Thanks,
Greg

PS. I am proactively moderating comments. Your comment WILL NOT APPEAR UNTIL I approve it. This may take some hours...