The ServerDocument object was aptly named. It was primarily designed for exactly the scenario we have just explored: writing information into a document on a server. However, it can do a lot more, from reading the data back out of a document to updating the deployment information inside a document, to adding customizations to documents. We discuss the portions of the ServerDocument object model used in deployment scenarios in Chapter 20, and spend the rest of this chapter describing the data-manipulating tools in the ServerDocument in more detail.
Let's take a look at another illustrative use of the ServerDocument object, and then we give a more complete explanation of all its data properties and methods. Here is a handy C# console application that dumps out the "cached data manifest" and serialized cached data in a document.
Listing 18-4. Creating a Cache Viewer with ServerDocument
using Microsoft.VisualStudio.Tools.Applications.Runtime; using System; using System.IO; using System.Text; namespace VSTOViewer { public class MainClass { public static void Main(string[] args) { if (args.Length != 1) { Console.WriteLine("Usage:"); Console.WriteLine(" CacheViewer.exe myfile.doc"); return; } string filename = args[0]; ServerDocument doc = null; try { doc = new ServerDocument(filename, false, FileAccess.Read); Console.WriteLine(" Cached Data Manifest"); Console.WriteLine(doc.CachedData.ToXml()); foreach(CachedDataHostItem view in doc.CachedData.HostItems) { foreach(CachedDataItem item in view.CachedData) { if (item.Xml != null && item.Xml.Length != 0) { Console.WriteLine(" Cached Data: " + view.Id + "." + item.Id + " xml "); Console.WriteLine(item.Xml); } if (item.Schema != null && item.Schema.Length != 0) { Console.WriteLine(" Cached Data: " + view.Id + "." + item.Id + " xsd "); Console.WriteLine(item.Schema); } } } } catch (CannotLoadManifestException ex) { Console.WriteLine("Not a customized document:" + filename); Console.WriteLine(ex.Message); } catch (FileNotFoundException) { Console.WriteLine("File not found:" + filename); } catch (Exception ex) { Console.WriteLine("Unexpected Exception:" + filename); Console.WriteLine(ex.ToString()); } finally { if (doc != null) doc.Close(); } } } }
After you compile this into a console application, you can run the console application on the command line and pass the name of the document you want to view. The document must have a saved VSTO data island in it for anything interesting to happen.
Now that you have an idea of how the ServerDocument object model is used, we can talk about it in more detail.
Part One. An Introduction to VSTO
An Introduction to Office Programming
Introduction to Office Solutions
Part Two. Office Programming in .NET
Programming Excel
Working with Excel Events
Working with Excel Objects
Programming Word
Working with Word Events
Working with Word Objects
Programming Outlook
Working with Outlook Events
Working with Outlook Objects
Introduction to InfoPath
Part Three. Office Programming in VSTO
The VSTO Programming Model
Using Windows Forms in VSTO
Working with Actions Pane
Working with Smart Tags in VSTO
VSTO Data Programming
Server Data Scenarios
.NET Code Security
Deployment
Part Four. Advanced Office Programming
Working with XML in Excel
Working with XML in Word
Developing COM Add-Ins for Word and Excel
Creating Outlook Add-Ins with VSTO