ServerDocument.Save Method ()


Saves any changes that were made to the document by using the ServerDocument class.

Namespace:   Microsoft.VisualStudio.Tools.Applications
Assembly:  Microsoft.VisualStudio.Tools.Applications.ServerDocument (in Microsoft.VisualStudio.Tools.Applications.ServerDocument.dll)

public void Save()

Exception Condition

The ServerDocument has been closed.


The Id property of a CachedDataHostItem is null or empty.


The Id property of a CachedDataItem is null or empty.


The DataType property of a CachedDataItem is null or empty.

The Save method saves any changes you have made to the cached data or to the deployment manifest URL in the document. If you created the ServerDocument object by using a document on disk, this method saves these changes to disk. If you created the ServerDocument object by using a document in memory, this method saves these changes to the memory buffer.

The following code example creates a new ServerDocument for a specified Excel workbook, modifies the value of a string that is cached in a worksheet by using the SerializeDataInstance method, and then saves the change by using the Save method.

This example requires:

  • A document-level customization for Excel that has a Sheet1 class in the ExcelWorkbook1 namespace, and a cached string in the Sheet1 class named CachedString.

  • A console application project or some other non-Office project.

  • References to the following assemblies:

    • Microsoft.VisualStudio.Tools.Applications.ServerDocument.dll and Microsoft.VisualStudio.Tools.Applications.Runtime.dll (if the project targets the .NET Framework 4 or the .NET Framework 4.5).


    • Microsoft.VisualStudio.Tools.Applications.ServerDocument.v10.0.dll and Microsoft.VisualStudio.Tools.Applications.Runtime.v9.0.dll (if the project targets the .NET Framework 3.5).

  • Imports (for Visual Basic) or using (for C#) statements for Microsoft.VisualStudio.Tools.Applications and Microsoft.VisualStudio.Tools.Applications.Runtime namespaces at the top of your code file.

private void ModifyCachedString(string documentPath)
    int runtimeVersion = 0;
    ServerDocument serverDocument1 = null;

        runtimeVersion = ServerDocument.GetCustomizationVersion(documentPath);

        if (runtimeVersion != 3)
            MessageBox.Show("This document does not have a Visual Studio Tools for " +
                "Office customization, or it has a customization that was created with " +
                "a version of the runtime that is incompatible with this version of the " +
                "ServerDocument class.");

        if (ServerDocument.IsCacheEnabled(documentPath))
            serverDocument1 = new ServerDocument(documentPath);
            CachedDataHostItem hostItem1 = 
            CachedDataItem dataItem1 = hostItem1.CachedData["CachedString"];

            if (dataItem1 != null &&
                Type.GetType(dataItem1.DataType) == typeof(string))
                dataItem1.SerializeDataInstance("This is the new cached string value.");
            MessageBox.Show("The specified document does not have cached data.");
    catch (System.IO.FileNotFoundException)
        System.Windows.Forms.MessageBox.Show("The specified document does not exist.");
    catch (UnknownCustomizationFileException)
        System.Windows.Forms.MessageBox.Show("The specified document has a file " +
            "extension that is not supported by Visual Studio Tools for Office.");
        if (serverDocument1 != null)
Return to top