Share via


Adding word document save as functionality to your SharePoint 2010 site

Word automation services in SharePoint 2010 provides you a “Save-As” experience on the server which otherwise would have required Office client installed. By default there is no UI integration available and you have to write custom code and use OM to interact with the engine, because of this reason lot of customers I have talked don’t quite know about the capability. This article will walk through steps I took to build a solution which provides UI integration to submit conversion requests as well as an administration page to monitor conversion requests. Administration page is integrated into central administration site via a custom action.

Getting started with word automation services development:

“Microsoft.Office.Server.Word” is the assembly to reference in your SharePoint project before you can start developing solutions that utilize word automation services. This assembly can be found in ISAPI directory under 14 hive.

assemblyRef

Submitting Conversion Requests:

Submitting conversion requests to word automation services engine is implemented as a custom SharePoint 2010 dialog page. Dialog page allows the user to specify name of the conversion job, output file format and output library to store converted files. One thing to keep in mind is conversion requests are always asynchronous. Screen below shows a “Save As” dialog page.

wordservices2

Code below submits a new conversion request to word services engine. Each document that was selected is added to the job for conversion to specified file format. Also if an output library is chosen we tell the engine to output the converted files in that library by calling the “AddLibrary” method on the “ConversionJob” class.

 void Submit(Guid sourceListGuid, SaveFormat saveFormat, string[] selectedIds)
 {
     ConversionJobSettings jobSettings = new ConversionJobSettings();
     jobSettings.OutputSaveBehavior = SaveBehavior.AlwaysOverwrite;
     jobSettings.OutputFormat = saveFormat;
  
     WordServiceApplicationProxy proxy = (WordServiceApplicationProxy)SPServiceContext.Current.GetDefaultProxy(typeof(WordServiceApplicationProxy));
     ConversionJob conversionJob = new ConversionJob(proxy, jobSettings);
     conversionJob.Name = jobNameTextBox.Text;
     //set user token & Site Subscription Ids
     conversionJob.UserToken = SPContext.Current.Web.CurrentUser.UserToken;
     if(SPContext.Current.Site.SiteSubscription != null)
         conversionJob.SubscriptionId = SPContext.Current.Site.SiteSubscription.Id;
  
     if (selectedIds.Length > 0)
     {
         SPList theList = SPContext.Current.Web.Lists[sourceListGuid];
         if (theList != null)
         {
             foreach (string id in selectedIds)
             {
                 //add files
                 SPListItem item = theList.Items.GetItemById(Convert.ToInt32(id));
                 string fileUrl = SPContext.Current.Web.Url + "/" + item.Url;
                 //only process docx files
                 if (Path.GetExtension(fileUrl).ToLower() == ".docx")
                 {
                     conversionJob.AddFile(fileUrl, Path.ChangeExtension(fileUrl, GetExtension(saveFormat)));
                 }
             }                    
             if(outputLibraryDropDownList.SelectedItem.Value != string.Empty)
             {
                 SPList outLibrary = SPContext.Current.Web.Lists[outputLibraryDropDownList.SelectedItem.Value];
                 //Specify conversion job where to output the converted documents
                 conversionJob.AddLibrary(theList, outLibrary);
             }
         }
         //start the job
         conversionJob.Start();
     }
 }

Ribbon Integration:

Custom ribbon button was created to provide integration to dialog page that was created earlier to provide save as functionality by submitting a conversion request to word automation services. All the selected Items are passed to the save as dialog page as a delimited string via querystring parameter, dialog page parses this string to form a collection of IDs which are then used to specify files that need to be converted to target file format selected in the dialog page. 

wordservices1

Monitoring Conversion Requests:

Conversion requests are asynchronous and object model is the only way to query status of the conversion request. A custom administration page uses OM to get all jobs and their statuses and displays them in an SPGridView control. This page is integrated into Central Administration site so portal administrators can monitor status of conversions. Screen below shows central administration site integration from “Monitoring” tab

wordservices4

See screen below shows conversion requests that successfully completed

wordservices5

Screen below shows failed job

wordservices6

Last bit of component to this solution is the feature activation handler we check to make sure the “Word Automation Services” service application is available.

wordservices7

I have added the solution to codeplex, you can find the source and the latest “wsp” here https://wordservicesuiext.codeplex.com/

Cheers,

</Ram>

Technorati Tags: SharePoint2010,Word