Clearing the Data Cache

Using ServerDocument, you can clear the cached data from the document. For example, suppose you have a VSTO solution that you want to deploy to an end user, but you don't want to remove the solution or the Runtime Storage Control because the user will run the VSTO solution. You do want to clear the data cache so that your test data does not go to the end user. Listing 13.16 shows how to clear the data cache from the document.

Listing 13.16. Clearing the data cache in a VSTO document Imports Microsoft.VisualStudio.Tools.Applications.Runtime

Module Module1

Sub Main()

' Path to VSTO document.

Dim documentPath As String = "C:\Invoice\Invoice.xls"

' Clear the data cache. ClearDataCache(documentPath, True)

End Sub

Sub ClearDataCache(ByVal documentPath As String, _ ByVal ClearDataOnly As Boolean)

' Create the ServerDocument class.

Dim myServerDocument As New ServerDocument(documentPath)

' Get the cached data from the document. Dim myCachedData As CachedData = myServerDocument _ .CachedData

If ClearDataOnly Then

' Clear only the data but leave the description. myCachedData.ClearData()


' Alternatively, you can call Clear to completely ' remove the data cache. myCachedData.Clear() End If myServerDocument.Save() myServerDocument.Close() End Sub

End Module

In the ClearDataCache subroutine you create a new instance of the ServerDocument class using the path to the document. You get a reference to the CachedData object in ServerDocument. You can then call Clear or ClearData. The Clear method clears all the data and removes the data cache description from the manifest. Calling the ClearData method clears only the data; it does not remove the description from the manifest. In Listing 13.16 a ClearDataCache subroutine takes a Boolean value that enables you to choose how you want the data cache cleared.

0 0

Post a comment