SharePoint Events

  5/13/2013 - Conference: SharePoint Summit 2013
  5/21/2013 - Webcast: SharePoint 2013 and ECM: Content Migration and Storage
  5/22/2013 - Webcast: Managing CAD in SharePoint
  5/23/2013 - Webcast: SharePoint Document Automation and E-Forms for Financial Services
  5/24/2013 - Webcast: What's New in Search for SharePoint 2013

 SharePoint Videos

  Why SharePoint 2013
  SharePoint 2013 Launch
  SharePoint 2013 Migration and Governance
  SharePoint 2013 and Enterprise Content Management
  Top Benefits of SharePoint 2013
  What's New in Business Intelligence in Office and SharePoint 2013
  SharePoint and Office 2013 Integration
  SharePoint 2013 Infrastructure Preview
  SharePoint, Lync, and, Exchange in the Cloud with Office 365
  Advanced Reporting in SharePoint with Microsoft Power View

 Archives

Opening SharePoint Links in a new windowUse SHIFT+ENTER to open the menu (new window).
Mail Enabled Lists vs. The Missing Windows 2008 POP3/IMAP Server Use SHIFT+ENTER to open the menu (new window).
7 Tools for SharePoint DevelopersUse SHIFT+ENTER to open the menu (new window).
Public Facing Masterpage TechniquesUse SHIFT+ENTER to open the menu (new window).
How to Quickly Deploy and Activate a Timer Service to Your Site CollectionUse SHIFT+ENTER to open the menu (new window).
Custom SharePoint Master Page Feature with WSP BuilderUse SHIFT+ENTER to open the menu (new window).
Date Math with InfoPathUse SHIFT+ENTER to open the menu (new window).
Enterprise Search Tricks and Tips Part 1Use SHIFT+ENTER to open the menu (new window).
Populating Word Documents With SharePoint Data. Try The DIP!Use SHIFT+ENTER to open the menu (new window).
Programmatic Deep Dive into Blank SharePoint Lookup ColumnsUse SHIFT+ENTER to open the menu (new window).
1 - 10 Next
Combining Open XML and Word Automation Services

By: Kathryn Bartlett

In this blog I will walk through the process of programmatically creating a Word document, using the Open XML SDK 2.0, uploading this document to SharePoint 2010, and then using Word Automation Services to convert this document into a PDF.  For this example, all of the code will be running from a Console Application created with Visual Studio 2010.

Creating the Console Application

First we must create the Console Application and make sure all of the appropriate References are added.  In Visual Studio 2010, this can be done easily by clicking on File -> New -> Project.

The_SharePoint_Blog_Combining_Open_XML_and_Word_Automation_Services

Then in the Windows section underneath Visual C#, select Console Application.  Make sure .NET Framework 3.5 is selected, enter a name, and hit OK.

The_SharePoint_Blog_Combining_Open_XML_and_Word_Automation_Services

 

Now we need to make sure the application’s Platform target is set to Any CPU.  To do this, right-click Properties and then Open:

 

The_SharePoint_Blog_Combining_Open_XML_and_Word_Automation_Services

Select the Build tab and make sure to set the Platform target to Any CPU.

The_SharePoint_Blog_Combining_Open_XML_and_Word_Automation_Services

Now that we’ve created the project successfully, we need to add the following references:

·         Microsoft.SharePoint

·         DocumentFormat.OpenXml

·         WindowsBase

·         Microsoft Office 2010 component

In order to add the DocumentFormat.OpenXML reference,  we need to download the OpenXML SDK 2.0.  This can be found at the following URL: http://www.microsoft.com/download/en/details.aspx?id=5124

Offered on this site is both a Productivity tool, as well as the actual SDK.  For purposes of this blog, all we need is the SDK.  The tool is helpful for generating more complex documents, as it allows one to reverse-engineer the code. 

To add these references, right-click References and select Add Reference…

The_SharePoint_Blog_Combining_Open_XML_and_Word_Automation_Services

Building the Code

As this is a simple example, we will work solely out of the Main method.   First we need to connect to SharePoint to get to our document library.  We also need to make sure the web is set to allow unsafe updates. 

The_SharePoint_Blog_Combining_Open_XML_and_Word_Automation_Services

Next we will build the code that will generate the Word document.  In order to upload into SharePoint, we need to create the document off a memory stream.  Using OpenXML, you create a WordprocessingDocument.  This WordprocessingDocument needs to contain a MainDocumentPart.  The MainDocumentPart will need to contain a Document.  The Document will need to contain a Body.  This body is where your paragraphs will lie.

The_SharePoint_Blog_Combining_Open_XML_and_Word_Automation_Services

Next, we will create two simple paragraphs.  As mentioned earlier, these will need to be appended to the Body.  Each paragraph needs to contain a Run which needs to contain Text.

The_SharePoint_Blog_Combining_Open_XML_and_Word_Automation_Services

These Paragraph, Run, and Text objects can contain properties to add styles, fonts, etc.  The productivity tool mentioned earlier makes determining what these properties should look like much simpler.

Now we need to upload this document into our SharePoint library. 

The_SharePoint_Blog_Combining_Open_XML_and_Word_Automation_Services

Using Word Automation Services, we will now convert this Word document into a PDF with the following code.

The_SharePoint_Blog_Combining_Open_XML_and_Word_Automation_Services

By default, the Word Automation Service Timer Job runs every 15 minutes.  Therefore, the PDFs you try to make will actually be created and uploaded into the library every 15 minutes – you will not see your PDF instantly upon running this code.  You can change the frequency with which this job runs by going into Central Administration.  Select Monitoring, then Review Job Definitions.  Click on the job Word Automation Services Timer Job and select the frequency appropriate for your scenario.

By: Kathryn Bartlett

        

Comments

There are no comments yet for this post.
Items on this list require content approval. Your submission will not appear in public views until approved by someone with proper rights. More information on content approval.

Your Name *


e-mail address *


Website (optional)


Comment *


Attachments

 Subscribe

  GigWerks RSS  Gig Werks Mailing List 

 Contact Us

 Connect

 Resources

  On Demand SharePoint Webcast Recordings
  Upcoming Webinars
  SharePoint Resources
  Business Intelligence Resources
  Gig Werks Website



©2009 Gig Werks. All rights reserved. Privacy Policy