Export (0) Print
Expand All

ServerDocument Class

Visual Studio 2005

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)

public sealed class ServerDocument : IDisposable

The ServerDocument class has two main purposes:

  • It enables you to access the cached data and application manifest in a document without starting Word or Excel.

  • It enables you to programmatically attach or remove customization assemblies.

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:

  • A set that you can use to access a document that has already been opened in memory.

  • A set that you can use to access a document that is on disk.

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 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.

Community Additions

ADD
Show:
© 2014 Microsoft