Provides access to the cached data and the application manifest in a Microsoft Office Word document or Microsoft Office Excel workbook.
Namespace:
Microsoft.VisualStudio.Tools.Applications.Runtime Assembly:
Microsoft.VisualStudio.Tools.Applications.Runtime (in Microsoft.VisualStudio.Tools.Applications.Runtime.dll)
Visual Basic (Declaration)
<PermissionSetAttribute(SecurityAction.Demand, Name := "FullTrust")> _
Public NotInheritable Class ServerDocument _
Implements IDisposable
Dim instance As ServerDocument
[PermissionSetAttribute(SecurityAction.Demand, Name = "FullTrust")]
public sealed class ServerDocument : IDisposable
The ServerDocument class has two main purposes:
To access the cached data or the application manifest in a document, you must first create a ServerDocument object by passing the document to one of the ServerDocument constructors. You can then access the cached data by using the CachedData property, and you can access the application manifest by using the AppManifest property. The application manifest provides information about the customization assemblies that are attached to the document. For more information, see Application and Deployment Manifests in Office Solutions.
To add or remove a customization, use the static AddCustomization and RemoveCustomization methods. These methods are useful if you want to add or remove a customization when the document is not on the development computer. You can call these methods only when the computer has Word or Excel installed, because these methods start Word and Excel to add or remove the customization.
Choosing Which Constructor to Use
There are two sets of ServerDocument constructors:
Accessing a Document in Memory
To access a document that has already been opened in memory, use one of the following constructors:
These constructors accept a byte array or a Stream that represents the document in memory. This is useful if you want to modify the cached data or application manifest in the document before streaming it to a destination by using the HTTP protocol. To use these constructors, the document must already have a Visual Studio Tools for Office customization; otherwise, these constructors will throw a CannotLoadManifestException exception.
Accessing a Document on Disk
To access a document that is on disk, use one of the following constructors:
These constructors accept the full path of the document that you want to open. The document must already have a Visual Studio Tools for Office customization.
By default, the document is opened with read/write access. If you want to open the document with read-only or write-only access, use the constructor that has a FileAccess parameter.
If you want to open a document that does not yet have a Visual Studio Tools for Office customization, use a constructor that has the Boolean onClient parameter. If you set this parameter to true, the Visual Studio Tools for Office runtime will create an empty Runtime Storage Control in the document. This is useful if you want to add cached data to a document that does not yet have a customization. The client computer must have Word or Excel installed before you can call the ServerDocument constructor, because the Visual Studio Tools for Office runtime starts Word or Excel to create the Runtime Storage Control. For more information about the Runtime Storage Control, see Runtime Storage Control Overview.
The following code example creates a new ServerDocument that loads a specified document and then displays the names of all the objects in the document's data cache. This example requires a reference to the Microsoft.VisualStudio.Tools.Applications.Runtime.dll assembly, and an Imports (for Visual Basic) or using (for C#) statement for the Microsoft.VisualStudio.Tools.Applications.Runtime namespace at the top of your code file.
Private Sub CreateServerDocumentOnClient(ByVal fileName As String)
If ServerDocument.IsCustomized(fileName) Then
Dim serverDocument1 As ServerDocument = Nothing
Try
serverDocument1 = New ServerDocument(fileName, True)
Dim stringBuilder1 As New System.Text.StringBuilder()
' Display all of the cached data items
' in the document.
Dim hostItem1 As CachedDataHostItem
For Each hostItem1 In serverDocument1.CachedData.HostItems
stringBuilder1.Append(vbLf + "Namespace and class: ")
stringBuilder1.Append(hostItem1.Id + vbLf)
Dim dataItem1 As CachedDataItem
For Each dataItem1 In hostItem1.CachedData
stringBuilder1.Append(" Data item: ")
stringBuilder1.Append(dataItem1.Id + vbLf)
Next dataItem1
Next hostItem1
MsgBox(stringBuilder1.ToString())
Finally
If Not serverDocument1 Is Nothing Then
serverDocument1.Close()
End If
End Try
Else
MsgBox("The specified document is not " + _
"customized.")
End If
End Sub
private void CreateServerDocumentOnClient(string fileName)
{
if (ServerDocument.IsCustomized(fileName))
{
ServerDocument serverDocument1 = null;
try
{
serverDocument1 = new ServerDocument(fileName,
true);
System.Text.StringBuilder stringBuilder1 =
new System.Text.StringBuilder();
// Display all of the cached data items
// in the document.
foreach (CachedDataHostItem hostItem1 in
serverDocument1.CachedData.HostItems)
{
stringBuilder1.Append("\nNamespace and class: ");
stringBuilder1.Append(hostItem1.Id + "\n");
foreach (CachedDataItem dataItem1 in
hostItem1.CachedData)
{
stringBuilder1.Append(" Data item: ");
stringBuilder1.Append(dataItem1.Id + "\n");
}
}
MessageBox.Show(stringBuilder1.ToString());
}
finally
{
if (serverDocument1 != null)
serverDocument1.Close();
}
}
else
{
MessageBox.Show("The specified document is not " +
"customized.");
}
}
System..::.Object
Microsoft.VisualStudio.Tools.Applications.Runtime..::.ServerDocument
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Concepts
Reference