Managed Code, Data and Microsoft Office Mike Pelton mpelton@microsoft.com Developer & Platform Group Microsoft Ltd.
General Information Work Flow assistive core content Storage Server process Storage Server process XML, data or documents Server Processing Create document Pre-populate template Search / extract Office programs Create content Analyze/review information Process documents Post-Office Processing Business workflow Collaborative work Publish/distribute/archive Microsoft Office 2003 Development Technologies XML VSTO v2005 data island Server-side Processing Web services IBF Research services ADO.NET Web queries Information Access Office U.I. Custom controls / U.I. Smart tags Smart documents XML schema Data binding Object models Research task pane Doc. actions task pane User Interaction Base Programmability XML Documents Data Post-Office Processing
Information Bridge Framework Smart Clients Web Service-based architecture (SOA) Centralized metadata/UI server Runtime for smart tag / XML event initiation Windows Forms and task pane support Web Services Metadata Finance ERP Web Services CRM
Information Bridge Architecture Development Only Office Application (Word, Excel, Outlook) Information Bridge Runtime (Office add-in) Task Pane Manager Office Integration U.I. Rendering Visual Studio Metadata Cache User Control Designer Attached Schema Smart Tag Metadata Designer U.I. (WinForm, XSLT) (SOAP) Metadata Service Metadata Database Client Server Web Service Optional: Information Bridge Compliant CRM Customer Information Invoices SAP Legacy Application Aggregation Service Orders Web services
Document Data Island Overview of the Data Island Stores program data in the document Why cache data in the document? Available when client is offline Accessible to a server process Store configuration info Data caching support in Visual Studio Tools for Office Fields marked with Cached attribute Types can implement ICachedType to provide custom serialization ServerDocument class accesses cache without Microsoft Word or Microsoft Excel The data cache is a new feature for Word and Excel that allows program data to be stored in the document independent of the actual document itself. There are a number of reasons to store data in the document. Because the data is able to persist between document runs, the cached data can be used when the client is offline. When online, the data can be retrieved and updated as necessary, and when offline the last data cached is still available. Also, the data in the document is accessible from outside of Word and Excel. A server-based solution can open the document and manipulate the cached data to add, update, or view information. Finally, because a document has no external configuration file to hold settings, the data cache can serve to keep this information associated in a readily available manner. VSTO 2005 supports data caching in a number of ways. A new attribute, CachedAttribute, is used to mark any fields in the assembly classes that should be cached in the document. For types that require custom serialization capabilities, the ICachedType interface can be implemented so it can be cached. Finally, the CustomizedDocument class is available without the need to host Word or Excel.
Document Data Island Example of Caching Data [Cached()] CustomerDataSet customerDataSet; void ThisDocument_Initialize(…) { if (customerDataSet == null) customerDataSet = new CustomerDataSet(); dataAdapter.Fill(customerDataSet); } list1.DataSource = customerDataSet; [Build 0]In this example, the _dsCustomer field is marked with the CachedAttribute. This means that its contents will be saved when the document is saved, and loaded when the document is loaded. Notice that the field is not initialized with a null or object reference. Doing so would negate the effect of the cache by always setting the field to that value. [Build 1]The ThisDocument_Initialize method is called when the document is first getting set up. This is the entry point and the most appropriate location for checking cached values. [Build 2] The first check is to see if the cached value has been set. If not, then this is likely the first time the document has been run. If that is the case, the application initializes the dataset and fills it with data. [Build 3] Once the cached value is ready (either due to being set previously or in this run), the dataset is used as the data source for a list box.
Document Data Island Using the Data Island Visual Studio Tools for Office provides the ability to cache data within documents Enables new server scenarios Pre-populates or extracts data, without having to program XML Excel or Word not instantiated Enables offline usage Uses cached lookup tables Requires no hidden sheets There are a number of benefits to having a cache for program data with the document. Documents can take advantage of live data that is stored with the document to create real-world applications. The data cache is also accessible from outside of the document. This can be used to enable server-side scenarios difficult or impossible to achieve previously. It is now possible to add data to the document or to extract data from the document without user intervention. A document could be pre-loaded with data before delivering it to a user, and data could be then extracted after the user interaction. Interacting with the data cache does not require Excel or Word to be launched or even installed. New managed classes enable working with the documents from a purely object-based approach.
Document Data Island Requesting Data from a Server ASPX page Data Island This diagram illustrates a client requesting a document-based resource from a server. [BUILD 0] When the server receives the request for the document, the server code connects to the appropriate database to retrieve data that will be needed by the document’s associated assembly. This data is then stored directly inside the document to be available to the managed code at runtime. [BUILD 1] The server code then streams out the document object back to the client. The stream contains the entire document with a pre-populated data island that contains the custom data. User data Server-side code can pre-populate elements before sending to client
Document Data Island Sending Data Back to a Server ASPX page Updated Data Cache Updated data [BUILD 0] This diagram demonstrates a document being sent back to the server after being manipulated by the client. All interaction with the document occurred through Word or Excel as a natural document-based program user interface. [BUILD 1] When the document is saved, the cached data is saved in the same file, creating one unit. [BUILD 2] The user then accesses a page on a Web site to upload the document. [BUILD 3] With no reliance on Word or Excel, the server code is able to open the document and access the data cache. From there data is saved to the appropriate database. A VSTO 2005 enabled document is posted to a server, data is extracted, and sent to corporate data sources
Document Data Cache Using the ServerDocument class private void GetData() { string user = Request.ServerVariables("AUTH_USER"); ExpensesDataset dsExpenses = new ExpensesDataset(); ExpensesDataAdapter.Fill(dsExpenses, user); ServerDocument doc = new ServerDocument("expenses.xls"); CachedDataItem cdi = doc.CachedDataManifest.Views ["ExpensesWorksheet"].CachedData["Expenses"]; cdi.SerializeDataInstance(dsExpenses); doc.Save(); doc.Close(); Response.Write(doc); } The server code manipulates the document using the CustomizedDocument class. In this example, an existing document is modified by adding a dataset to its cache. The CustomizedDocument class is instantiated with an existing document. Once created, the individual cache items can be extracted using the CachedDataManifest property. This references one or more views contained in the document. By default, Microsoft Word documents contain one view, ThisDocument. Excel documents contain named views for each worksheet: Sheet1, Sheet2, etc. These view names can be changed when the document is authored. Within the view, each data item is referenced using the CachedData property and the data item’s name. This name is based on the variable name of the field as declared in the code for the document’s assembly. Once a reference to the data item is obtained, the contents can be accessed or modified. In this example, the SerializeDataInstance method serializes the dsExpenses object and stores the bytes in the document’s data cache. An alternative to serializing the data in this manner is to directly encode the data as XML and use the XML property of CachedData to set it. The document is then saved and closed, then written back to the client.
All the books you’ll ever need: Microsoft .NET Development for Microsoft Office By Andrew Whitechapel
The slides for this event will be posted at: www.microsoft.com/uk/msdn/postevents
Get the latest technology previews, trial software, special offers Get information tailored to your needs Pick your RSS feeds Sign up for MSDN Connection at: http://www.microsoft.com/uk/msdn
Smartclient competition http://www.microsoftsmartcar.com It’s a Road Trip! http://www.microsoft.com/uk/msdn/roadtrip
Additional Information Office Site http://www.microsoft.com/office The UK MSDN Site & Flash Local news, events, webcasts http://www.microsoft.com/uk/msdn Register to receive the bi-weekly MSDN Flash by email http://www.microsoft.com/uk/msdn/flash.aspx Try Visual Studio http://www.microsoft.com/vstudio/tryit Take a look at the Express products http://msdn.microsoft.com/express GotDotNet and ASP.NET – lots of excellent resources http://www.gotdotnet.com http://www.asp.net
© 2003 Microsoft Limited. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.