Document Content Example

The following code example demonstrates how to update the content of an existing document. This example assumes a web reference named CEWS_SOAP has been set to the following URI: http://localhost:<port>/wsi/FNCEWS35SOAP/wsdl.

using System;
using System.IO;
using Microsoft.Web.Services2;
using Microsoft.Web.Services2.Dime;
using Microsoft.Web.Services2.Security.Tokens;
using documentcontent_ex.CEWS_SOAP;

namespace documentcontent_ex
{
   /// <summary>
   /// Update the content of a document.
   /// </summary>
   class Class1
   {
      /// <summary>
      /// The main entry point for the application
      /// </summary>
      [STAThread]
      static void Main(string[] args)
      {
         //************************************************************************
         // This code example demonstrates how to check out a document, update   
         // its content and then check it back in.
         //************************************************************************

         const string url = "http://localhost:9080/wsi/FNCEWS35SOAP/";  // Change port number if necessary
         const string objectStore = "ContentEngineExs";  // Pre-existing object store
         const string documentPath = "/TestFolder/DemoDoc";  // Path to pre-existing document in the object store
         const string saveContentPath = "c:\\Test\\UploadContent.doc";  // Path to pre-existing file containing the content to upload
   
         // Create a WSE-enabled web service object to provide access to SOAP header   
         FNCEWS35ServiceWse wseService = new FNCEWS35ServiceWse();
         wseService.Url = url;
         SoapContext soapContext = wseService.RequestSoapContext;
         
         // Add security token to SOAP header with your username and password
         UsernameToken token = new UsernameToken("username", "password", PasswordOption.SendPlainText);
         soapContext.Security.Tokens.Add(token);
         
         // Add default locale info to SOAP header
         Localization defaultLocale = new Localization();
         defaultLocale.Locale = "en-US";
         
         // Build the Checkout action
         CheckoutAction verbCheckout = new CheckoutAction();
         verbCheckout.reservationType = ReservationType.Exclusive;
         verbCheckout.reservationTypeSpecified = true;
         
         // Assign the action to the ChangeRequestType element
         ChangeRequestType elemChangeRequestType = new ChangeRequestType();
         elemChangeRequestType.Action = new ActionType[1];
         elemChangeRequestType.Action[0] = (ActionType)verbCheckout;
         
         // Set a reference to the document to check out
         ObjectSpecification objDocument = new ObjectSpecification();
         objDocument.classId = "Document";
         objDocument.path = documentPath;
         objDocument.objectStore = objectStore;
   
         // Specify the target object (a document) for the actions
         elemChangeRequestType.TargetSpecification = objDocument;
         elemChangeRequestType.id = "1";
         
         // Create a property filter to get Reservation property
         PropertyFilterType elemPropFilter = new PropertyFilterType();
         elemPropFilter.maxRecursion = 5;
         elemPropFilter.maxRecursionSpecified = true;
         
         // Create filter element array to hold IncludeProperties specifications
         elemPropFilter.IncludeProperties = new FilterElementType[5];
         
         // Create filter element for CurrentVersion property
         elemPropFilter.IncludeProperties[0] = new FilterElementType();
         elemPropFilter.IncludeProperties[0].Value = "CurrentVersion";
         
         // Create filter element for Reservation property
         elemPropFilter.IncludeProperties[1] = new FilterElementType();
         elemPropFilter.IncludeProperties[1].Value = "Reservation";
         
         // Create filter element for ContentElements property
         elemPropFilter.IncludeProperties[2] = new FilterElementType();
         elemPropFilter.IncludeProperties[2].Value = "ContentElements";
         
         // Create filter element for Content pseudo-property
         elemPropFilter.IncludeProperties[3] = new FilterElementType();
         elemPropFilter.IncludeProperties[3].Value = "Content";
         elemChangeRequestType.RefreshFilter= elemPropFilter;
         
         // Create array of ChangeRequestType elements and assign ChangeRequestType element to it 
         ChangeRequestType[] elemChangeRequestTypeArray = new ChangeRequestType[1];
         elemChangeRequestTypeArray[0] = elemChangeRequestType;
         
         // Create ChangeResponseType element array 
         ChangeResponseType[] elemChangeResponseTypeArray;
   
         // Build ExecuteChangesRequest element and assign ChangeRequestType element array to it
         ExecuteChangesRequest elemExecuteChangesRequest = new ExecuteChangesRequest(); 
         elemExecuteChangesRequest.ChangeRequest = elemChangeRequestTypeArray;
         elemExecuteChangesRequest.refresh = true; // return a refreshed object
         elemExecuteChangesRequest.refreshSpecified = true;
   
         try
         {
            // Call ExecuteChanges operation to implement the document checkout
            elemChangeResponseTypeArray = wseService.ExecuteChanges(elemExecuteChangesRequest);
         }
         catch(System.Net.WebException ex)
         {
            Console.WriteLine("An exception occurred while checking in a document: [" + ex.Message + "]");
            return;
         }
		 
         // Return the Document object, unless there is an error
         if (elemChangeResponseTypeArray == null || elemChangeResponseTypeArray.Length < 1)
         {
            Console.WriteLine("A valid Document object was not returned from the ExecuteChanges operation");
            return;
         }
         
         // Get the CurrentVersion property from Document object
         SingletonObject prpCurrentVersion = null;
         foreach (PropertyType prpProperty in elemChangeResponseTypeArray[0].Property)
         {
            if (prpProperty.propertyId == "CurrentVersion")
            {
               prpCurrentVersion = (SingletonObject)prpProperty;
            }
         }   
         
         // Get current version Document object from CurrentVersion property value
         SingletonObject prpReservation = null;
         ObjectReference objCurrentVersion = (ObjectReference) prpCurrentVersion.Value;
         
         // Get Reservation property from Document object
         foreach (PropertyType prpProperty in elemChangeResponseTypeArray[0].Property)
         {
            if (prpProperty.propertyId == "Reservation")
            {
               prpReservation = (SingletonObject)prpProperty;
            }
         }
         
         // Get Reservation object from Reservation property value
         ObjectValue objReservation = (ObjectValue) prpReservation.Value; 
         
         // Get the ContentElements property from Document object
         ListOfObject prpContentElements = null;
         foreach (PropertyType prpProperty in elemChangeResponseTypeArray[0].Property)
         {
            if (prpProperty.propertyId == "ContentElements")
            {
               prpContentElements = (ListOfObject)prpProperty;
            }
         }
         
         // Get the content from each content element
         int elementCount = (prpContentElements.Value == null) ? 0 : prpContentElements.Value.Length;
         for (int i = 0; i < elementCount; i++)
         {
             // Get each ContentTransfer object in the ContentElements property collection
            DependentObjectType objContentTransfer = prpContentElements.Value[i];
            
            // Get Content pseudo-property from the ContentTransfer object
            ContentData prpContent = null;
            foreach (PropertyType prpProperty in objContentTransfer.Property)
            {
                if (prpProperty.propertyId == "Content")
                {
                   prpContent = (ContentData)prpProperty;
                }
            }
            
            // Get inline content data from Content property value
            InlineContent elemInlineContent = prpContent.Value as InlineContent;
            
            // Write inline content data to file
            Stream outputStream = File.OpenWrite("saveContentPath");
            outputStream.Write(elemInlineContent.Binary, 0, elemInlineContent.Binary.Length);
            outputStream.Close();
         }

         // *************************************************
         // Update document content without check-in.
         // *************************************************

         // Build the Update action
         UpdateAction verbUpdateAction = new UpdateAction();
         
         // Assign Update action to the ChangeRequestType element         
         ChangeRequestType elemUpdateChangeRequestType = new ChangeRequestType();
         elemUpdateChangeRequestType.Action = new ActionType[1];
         elemUpdateChangeRequestType.Action[0] = (ActionType)verbUpdateAction;
   
         // Specify the target (Reservation object) for the actions
         elemUpdateChangeRequestType.TargetSpecification = new ObjectReference(); 
         elemUpdateChangeRequestType.TargetSpecification.classId = "Document";
         elemUpdateChangeRequestType.TargetSpecification.objectId = objReservation.objectId; 
         elemUpdateChangeRequestType.TargetSpecification.objectStore = objectStore;
         elemUpdateChangeRequestType.id = "1";

         // Build a list of properties to set in the new document
         ModifiablePropertyType[] elemInputProps = new ModifiablePropertyType[2];
         
         // Specify and set a string-valued property for the DocumentTitle property
         SingletonString prpDocumentTitle = new SingletonString();
         prpDocumentTitle.Value = "out";
         prpDocumentTitle.propertyId = "DocumentTitle"; 
         elemInputProps[0] = prpDocumentTitle; // Add to property list
         
         // Create an object reference to dependently persistable ContentTransfer object
         DependentObjectType objContentTransferNew = new DependentObjectType();
         objContentTransferNew.classId = "ContentTransfer";
         objContentTransferNew.dependentAction = DependentObjectTypeDependentAction.Insert;
         objContentTransferNew.dependentActionSpecified = true;
         objContentTransferNew.Property = new PropertyType[2];
         
         // Create reference to ContentElements property and set its value to ContentTransfer object
         ListOfObject prpContentElementsNew = new ListOfObject();
         prpContentElementsNew.propertyId = "ContentElements";
         prpContentElementsNew.Value = new DependentObjectType[1];
         prpContentElementsNew.Value[0] = objContentTransferNew; 
         
         // Read data stream from file containing the document content
         InlineContent elemInlineContentNew = new InlineContent();
         System.IO.Stream inputStream = System.IO.File.OpenRead(saveContentPath);
         elemInlineContentNew.Binary = new byte[inputStream.Length];
         inputStream.Read(elemInlineContentNew.Binary, 0, (int)inputStream.Length);
         inputStream.Close();
   
         // Create reference to Content pseudo-property
         ContentData prpContentUpdate = new ContentData();
         prpContentUpdate.Value = (ContentType)elemInlineContentNew;
         prpContentUpdate.propertyId = "Content";
         
         // Assign Content property to ContentTransfer object 
         objContentTransferNew.Property[0] = prpContentUpdate;
         
         // Create and assign ContentType string-valued property to ContentTransfer object
         SingletonString prpContentType = new SingletonString();
         prpContentType.propertyId = "ContentType";
         prpContentType.Value = "application/msword"; // Set MIME-type to MS Word
         objContentTransferNew.Property[1] = prpContentType;
         elemInputProps[1] = prpContentElementsNew;
         
         // Assign list of document properties to set in ChangeRequestType element
         elemUpdateChangeRequestType.ActionProperties = elemInputProps;

         // Create array of ChangeRequestType elements and assign ChangeRequestType element to it 
         ChangeRequestType[] elemUpdateChangeRequestTypeArray = new ChangeRequestType[1];
         elemUpdateChangeRequestTypeArray[0] = elemUpdateChangeRequestType;
         ChangeResponseType[] elemUpdateChangeResponseTypeArray;
         
         // Create ChangeResponseType element array 
         ExecuteChangesRequest elemUpdateExecuteChangesRequest = new ExecuteChangesRequest(); 
         elemUpdateExecuteChangesRequest.ChangeRequest = elemUpdateChangeRequestTypeArray;
         elemUpdateExecuteChangesRequest.refresh = true; // return a refreshed object
         elemUpdateExecuteChangesRequest.refreshSpecified = true;
   
         try
         {
            // Call ExecuteChanges operation to implement the document Update action
            elemUpdateChangeResponseTypeArray = wseService.ExecuteChanges(elemUpdateExecuteChangesRequest);
         }
         catch(System.Net.WebException ex)
         {
            Console.WriteLine("An exception occurred while updating a document: [" + ex.Message + "]");
            return;
         }
         
         // Return the Document object, unless there is an error
         if (elemUpdateChangeResponseTypeArray==null || elemUpdateChangeResponseTypeArray.Length < 1)
         {
            Console.WriteLine("A valid Document object was not returned from the ExecuteChanges operation");
            return;
         }
         Console.WriteLine("Content uploaded to document '{0}'.", documentPath);
      
         // **********************************
         // Check in the document. 
         // **********************************

         // Build the Checkin action
         CheckinAction verbCheckin = new CheckinAction();
            
         // Assign the action to the ChangeRequestType element
         ChangeRequestType elemCheckinChangeRequestType = new ChangeRequestType();
         elemCheckinChangeRequestType.Action = new ActionType[1];
         elemCheckinChangeRequestType.Action[0] = (ActionType)verbCheckin;
   
         // Specify the target object (Reservation object) for the actions
         elemCheckinChangeRequestType.TargetSpecification = new ObjectReference(); 
         elemCheckinChangeRequestType.TargetSpecification.classId = "Document";
         elemCheckinChangeRequestType.TargetSpecification.objectId = objReservation.objectId; 
         elemCheckinChangeRequestType.TargetSpecification.objectStore = objectStore;
         elemCheckinChangeRequestType.id = "1";
            
         // Assign ChangeRequestType element
         ChangeRequestType[] elemCheckinChangeRequestTypeArray = new ChangeRequestType[1];
         elemCheckinChangeRequestTypeArray[0] = elemCheckinChangeRequestType;
         ChangeResponseType[] elemCheckinChangeResponseTypeArray;
            
         // Build ExecuteChangesRequest element and assign ChangeRequestType element array to it
         ExecuteChangesRequest elemCheckinExecuteChangesRequest = new ExecuteChangesRequest(); 
         elemCheckinExecuteChangesRequest.ChangeRequest = elemCheckinChangeRequestTypeArray;
         elemCheckinExecuteChangesRequest.refresh = true; // return a refreshed object
         elemCheckinExecuteChangesRequest.refreshSpecified = true;
   
         try
         {
            // Call ExecuteChanges operation to implement the doc checkin
            elemCheckinChangeResponseTypeArray = wseService.ExecuteChanges(elemCheckinExecuteChangesRequest);
         }
         catch(System.Net.WebException ex)
         {
            Console.WriteLine("An exception occurred while checking in a document: [" + ex.Message + "]");
            return;
         }
            
         // Return the Document object, unless there is an error
         if (elemCheckinChangeResponseTypeArray == null || elemCheckinChangeResponseTypeArray.Length < 1)
         {
            Console.WriteLine("A valid Document object was not returned from the ExecuteChanges operation");
            return;
         }
         Console.WriteLine("Checked-in Document '{0}'.", documentPath);
      }
   }
}