Freigeben über


Word Automation Services: Deleting Source Files after Conversion using VB

This is the complete sample for deleting source files after conversion from the article Developing with SharePoint 2010 Word Automation Services.

This blog is inactive.
New blog: EricWhite.com/blog

Blog TOCImports System.Collections.ObjectModel
Imports System.Threading
Imports Microsoft.SharePoint
Imports Microsoft.Office.Word.Server.Conversions
Module Module1
    Sub Main()
        Dim siteUrl As String = "https://localhost"
        ' If you manually installed Word Automation Services, then replace the name
        ' in the following line with the name that you assigned to the service when
        ' you installed it.
        Dim wordAutomationServiceName = "Word Automation Services"
        Using spSite As SPSite = New SPSite(siteUrl)
            Console.WriteLine("Starting conversion job")
            Dim job As ConversionJob = New ConversionJob(wordAutomationServiceName)
            job.UserToken = spSite.UserToken
            job.Settings.UpdateFields = True
            job.Settings.OutputFormat = SaveFormat.PDF
            job.Settings.OutputSaveBehavior = SaveBehavior.AlwaysOverwrite
            Dim folderToConvert As SPFolder = spSite.RootWeb.GetFolder("Shared Documents")
            job.AddFolder(folderToConvert, folderToConvert, False)
            job.Start()
            Console.WriteLine("Conversion job started")
      Dim status As ConversionJobStatus = _
                New ConversionJobStatus(wordAutomationServiceName, job.JobId, Nothing)
            Console.WriteLine("Number of documents in conversion job: {0}", status.Count)
            While True
            Thread.Sleep(5000)
                status = New ConversionJobStatus(wordAutomationServiceName, job.JobId, _
                                                 Nothing)
                If status.Count = status.Succeeded + status.Failed Then
              Console.WriteLine("Completed, Successful: {0}, Failed: {1}", _
                                      status.Succeeded, status.Failed)
                    Console.WriteLine("Deleting only items that successfully converted")
                    Dim convertedItems As ReadOnlyCollection(Of ConversionItemInfo) = _
                        status.GetItems(ItemTypes.Succeeded)
                    For Each convertedItem In convertedItems
                        Console.WriteLine("Deleting item: Name:{0}", convertedItem.InputFile)
                        folderToConvert.Files.Delete(convertedItem.InputFile)
                    Next
                    Exit While
                End If
                Console.WriteLine("In progress, Successful: {0}, Failed: {1}",
                                  status.Succeeded, status.Failed)
            End While
        End Using
    End Sub
End Module