Showing posts with label SharePoint Server 2010. Show all posts
Showing posts with label SharePoint Server 2010. Show all posts

Friday, 16 December 2011

Converting Word Documents to PDF Using SharePoint Server 2010 and Word Automation Services

Summary:   Learn to programmatically convert Word documents to PDF format on the server by using Word Automation Services with SharePoint Server 2010.

Applies to:   Microsoft SharePoint Server 2010 | Microsoft Business Connectivity Services| Microsoft Visual Studio 2010






OverviewSharePoint 2010 Word Automation Services available with SharePoint Server 2010 supports converting Word documents to other formats. This includes PDF. This article describes using a document library list item event receiver to call Word Automation Services to convert Word documents to PDF when they are added to the list. The event receiver checks whether the list item added is a Word document. If so, it creates a conversion job to create a PDF version of the Word document and pushes the conversion job to the Word Automation Services conversion job queue.

Code ItThis article describes the following steps to show how to call the Word Automation Services to convert a document:

  1. Creating a SharePoint 2010 list definition application solution in Visual Studio 2010.

  2. Adding a reference to the Microsoft.Office.Word.Server assembly.

  3. Adding an event receiver.

  4. Adding the sample code to the solution.


 

Creating a SharePoint 2010 List Definition Application in Visual Studio 2010

This article uses a SharePoint 2010 list definition application for the sample code.

To create a SharePoint 2010 list definition application in Visual Studio 2010




  1. Start Microsoft Visual Studio 2010 as an administrator.

  2. From the File Menu, point to the Project menu and then click New.

  3. In the New Project dialog box select the Visual C# SharePoint 2010 template type in the Project Templates pane.

  4. Select List Definition in the Templates pane.

  5. Name the project and solution ConvertWordToPDF.

    Figure 1. Creating the Solution

    Creating the solution

  6. To create the solution, click OK.

  7. Select a site to use for debugging and deployment.

  8. Select the site to use for debugging and the trust level for the SharePoint solution.










    note Note:
    Make sure to select the trust level Deploy as a farm solution. If you deploy as a sandboxed solution, it does not work because the solution uses the Microsoft.Office.Word.Server assembly. This assembly does not allow for calls from partially trusted callers.

     


    Figure 2. Selecting the trust level

    Creating the solution

  9. To finish creating the solution, click Finish.



Adding a Reference to the Microsoft Office Word Server Assembly

To use Word Automation Services, you must add a reference to the Microsoft.Office.Word.Server to the solution.

To add a reference to the Microsoft Office Word Server Assembly




  1. In Visual Studio, from the Project menu, select Add Reference.

  2. Locate the assembly. By using the Browse tab, locate the assembly. The Microsoft.Office.Word.Server assembly is located in the SharePoint 2010 ISAPI folder. This is usually located at C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI. After the assembly is located, click OK to add the reference.

    Figure 3. Adding the Reference

    Adding the reference



Adding an Event Receiver

This article uses an event receiver that uses the Microsoft.Office.Word.Server assembly to create document conversion jobs and add them to the Word Automation Services conversion job queue.

To add an event receiver




  1. In Visual Studio, on the Project menu, click Add New Item.

  2. In the Add New Item dialog box, in the Project Templates pane, click the Visual C# SharePoint 2010 template.

  3. In the Templates pane, click Event Receiver.

  4. Name the event receiver ConvertWordToPDFEventReceiver and then click Add.

    Figure 4. Adding an Event Receiver

    Adding an event receiver

  5. The event receiver converts Word Documents after they are added to the List. Select the An item was added item from the list of events that can be handled.

    Figure 5. Choosing Event Receiver Settings

    Choosing even receiver settings

  6. Click Finish to add the event receiver to the project.



Adding the Sample Code to the Solution

Replace the contents of the ConvertWordToPDFEventReceiver.cs source file with the following code.






using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;

using Microsoft.Office.Word.Server.Conversions;

namespace ConvertWordToPDF.ConvertWordToPDFEventReceiver
{
/// <summary>
/// List Item Events
/// </summary>
public class ConvertWordToPDFEventReceiver : SPItemEventReceiver
{
/// <summary>
/// An item was added.
/// </summary>
public override void ItemAdded(SPItemEventProperties properties)
{
base.ItemAdded(properties);

// Verify the document added is a Word document
// before starting the conversion.
if (properties.ListItem.Name.Contains(".docx")
|| properties.ListItem.Name.Contains(".doc"))
{
//Variables used by the sample code.
ConversionJobSettings jobSettings;
ConversionJob pdfConversion;
string wordFile;
string pdfFile;

// Initialize the conversion settings.
jobSettings = new ConversionJobSettings();
jobSettings.OutputFormat = SaveFormat.PDF;

// Create the conversion job using the settings.
pdfConversion =
new ConversionJob("Word Automation Services", jobSettings);

// Set the credentials to use when running the conversion job.
pdfConversion.UserToken = properties.Web.CurrentUser.UserToken;

// Set the file names to use for the source Word document
// and the destination PDF document.
wordFile = properties.WebUrl + "/" + properties.ListItem.Url;
if (properties.ListItem.Name.Contains(".docx"))
{
pdfFile = wordFile.Replace(".docx", ".pdf");
}
else
{
pdfFile = wordFile.Replace(".doc", ".pdf");
}

// Add the file conversion to the conversion job.
pdfConversion.AddFile(wordFile, pdfFile);

// Add the conversion job to the Word Automation Services
// conversion job queue. The conversion does not occur
// immediately but is processed during the next run of
// the document conversion job.
pdfConversion.Start();

}
}
}
}





 

Read ItWord Automation Services provided with SharePoint Server 2010 enables you to create server-based document solutions. Combining the functionality that is provided by Word Automation Services with the document content manipulation support provided with the Open XML SDK enables you to create rich document solutions that execute on the server that do not require Automation of the Word client application.

Examples of the kinds of operations supported by Word Automation Services are as follows:

  • Converting between document formats (e.g. DOC to DOCX)

  • Converting to fixed formats (e.g. PDF or XPS)

  • Updating fields

  • Importing "alternate format chunks"


This article contains sample code that shows how to create a SharePoint list event handler that can create Word Automation Services conversion jobs in response to Word documents being added to the list. This section uses code examples taken from the complete, working sample code provided earlier in this article to describe the approach taken by this article.

The ItemAdded event handler in the list event handler first verifies that the item added to the document library list is a Word document by checking the name of the document for the .doc or .docx file name extension.






// Verify the document added is a Word document
// before starting the conversion.
if (properties.ListItem.Name.Contains(".docx")
|| properties.ListItem.Name.Contains(".doc"))
{





If the item is a Word document then the code creates and initializes ConversionJobSettings and ConversionJob objects to convert the document to the PDF format.






//Variables used by the sample code.
ConversionJobSettings jobSettings;
ConversionJob pdfConversion;
string wordFile;
string pdfFile;

// Initialize the conversion settings.
jobSettings = new ConversionJobSettings();
jobSettings.OutputFormat = SaveFormat.PDF;

// Create the conversion job using the settings.
pdfConversion =
new ConversionJob("Word Automation Services", jobSettings);

// Set the credentials to use when running the conversion job.
pdfConversion.UserToken = properties.Web.CurrentUser.UserToken;





The Word document to be converted and the name of the PDF document to be created are added to the ConversionJob.






// Set the file names to use for the source Word document
// and the destination PDF document.
wordFile = properties.WebUrl + "/" + properties.ListItem.Url;
if (properties.ListItem.Name.Contains(".docx"))
{
pdfFile = wordFile.Replace(".docx", ".pdf");
}
else
{
pdfFile = wordFile.Replace(".doc", ".pdf");
}

// Add the file conversion to the Conversion Job.
pdfConversion.AddFile(wordFile, pdfFile);





Finally the ConversionJob is added to the Word Automation Services conversion job queue.






// Add the conversion job to the Word Automation Services 
// conversion job queue. The conversion does not occur
// immediately but is processed during the next run of
// the document conversion job.
pdfConversion.Start();




Wednesday, 3 August 2011

Back up a service application (SharePoint Server 2010)


Back up a service application (SharePoint Server 2010)

We recommend that you regularly back up at the farm level. However, business or IT requirements might require that you back up a service application. Regularly backing up a service application reduces the possibility of data losses that might occur from hardware failures, power outages, or other problems. It is a simple process that helps to ensure that all the service application-related data and configurations are available for recovery, if that is required. You can back up one service application at a time, or you can back up all service applications at once. For information about what to back up and which tools to use, see Plan for backup and recovery (SharePoint Server 2010). For more information, see Back up a farm (SharePoint Server 2010).

Backing up a service application does not affect the state of the farm. However, it does require resources. Therefore, backing up a service application might affect farm performance while the backup is running. You can avoid performance issues by backing up the service application during hours when farm use is lowest.










note Note:
SharePoint Server 2010 backup backs up remote Binary Large Object (BLOB) stores but only if you are using the FILESTREAM remote BLOB store provider to put data in remote BLOB stores.If you are using another provider, you must manually back up the remote BLOB stores.

 


Procedures in this topic:











note Note:
You cannot use SQL Server tools or Data Protection Manager to back up a service application.

 


Task requirements




Before you begin, you must create a folder on the local computer or the network in which to store the backups. For better performance, we recommend that you back up to the local computer and then move the backup files to a network folder. For more information about how to create a backup folder, see Prepare to back up and recover (SharePoint Server 2010).










note Note:
Microsoft SharePoint Server 2010 backup backs up the Business Data Connectivity service external content type definitions but does not back up the data source itself. To protect the data, you should back up the data source when you back up the Business Data Connectivity service or the farm.If you back up the Business Data Connectivity service or the farm and then restore the data source to a different location, you must change the location information in the external content type definition. If you do not, the Business Data Connectivity service might not be able to locate the data source.

 



Use Windows PowerShell to back up a service application




You can use Windows PowerShell to back up one or more service applications manually or as part of a script that can be run at scheduled intervals.

To back up a service application by using Windows PowerShell




  1. Verify that you meet the following minimum requirements: See Add-SPShellAdmin.

  2. On the Start menu, click All Programs.

  3. Click Microsoft SharePoint 2010 Products.

  4. Click SharePoint 2010 Management Shell.

  5. At the Windows PowerShell command prompt, type the following command:Backup-SPFarm -Directory <BackupFolder> -BackupMethod {Full | Differential} -Item <ServiceApplicationName> [-Verbose]

    Where:

    • <BackupFolder> is the path of a folder on the local computer or on the network in which you want to store the backups.

    • <ServiceApplicationName> is the name of the service application that you want to back up.












    note Note:
    To back up all the service applications, at the Windows PowerShell command prompt, type the following command:Backup-SPFarm -Directory <BackupFolder> -BackupMethod {Full | Differential} -Item "FarmShared Service Applications" [-Verbose]

     












    note Note:
    If you are backing up the service application for the first time, you must use the Fulloption. You must perform a full backup before you can perform a differential backup.Some service applications always require a full backup. For these service applications, even if you select the Differential option, the system performs a full backup.

     



For more information, see Backup-SPFarm.










note Note:
We recommend that you use Windows PowerShell when performing command-line administrative tasks. The Stsadm command-line tool has been deprecated, but is included to support compatibility with previous product versions.

 




Use Central Administration to back up a service application




You can use Central Administration to back up a service application.

To back up a service application by using Central Administration




  1. Verify that the user account that performs this procedure is a member of the Farm Administrators group.

  2. In Central Administration, on the Home page, in the Backup and Restore section, click Perform a backup.

  3. On the Perform a Backup — Step 1 of 2: Select Component to Back Up page, select the service application from the list of components, and then click Next. To back up all the service applications, select the Shared Service Applicationsnode.










    note Note:
    The service application might consist of several components. You must select the top-level component.

     


  4. On the Start Backup — Step 2 of 2: Select Backup Options page, in the Backup Type section, select either Full or Differential.










    note Note:
    If you are backing up the service application for the first time, you must use the Fulloption. You must perform a full backup before you can perform a differential backup.Some service applications always require a full backup. For these service applications, the system performs a full backup even if you select the Differential option.

     


  5. In the Backup File Location section, in the Backup location box, type the path of the backup folder, and then click Start Backup.

  6. You can view the general status of all backup jobs at the top of the Backup and Restore Job Status page in the Readiness section. You can view the status for the current backup job in the lower part of the page in the Backup section. The status page updates every 30 seconds automatically. You can manually update the status details by clicking Refresh. Backup and recovery are Timer service jobs. Therefore, it may take several seconds for the backup to start.If you receive any errors, you can review them in the Failure Message column of the Backup and Restore Job Status page. You can also find more details in the Spbackup.log file at the UNC path that you specified in step 5.