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 has been set to the following URI: http://localhost:<port>/wsi/FNCEWS40SOAP/wsdl.

using System;
using System.IO;    
using Microsoft.Web.Services3;
using Microsoft.Web.Services3.Security.Tokens;
using Microsoft.Web.Services3.Design;
using documentcontent_ex.CEWS;
 
namespace documentcontent_ex
{
   /// <summary>
   /// Check out a document, update its content and check it back in.
   /// </summary>
   class Class1
   {
      static void Main(string[] args)
      {
         const string strUrl = "http://localhost:9080/wsi/FNCEWS40SOAP/"; // Change port number if necessary
         const string strObjStoreName = "ContentEngineExs"; // Pre-existing object store
         const string strDocPath = "/TestFolder/DemoDoc";  // Path to pre-existing document in the object store
         const string strSaveContentPath = "c:\\Test\\UploadContent.doc";  // Path to pre-existing file containing the content to upload
       
         // Create a wse-enabled web service object
         FNCEWS40ServiceWse wseService = new FNCEWS40ServiceWse();
		 
         // Set security token with your username and password
         UsernameToken token = new UsernameToken("username", "password", PasswordOption.SendPlainText);
         wseService.SetClientCredential(token);
         Policy MyPolicy = new Policy();
         MyPolicy.Assertions.Add(new UsernameOverTransportAssertion());
         wseService.SetPolicy(MyPolicy);
         wseService.Url = strUrl;

         // Add default locale info to SOAP header
         Localization objDefaultLocale = new Localization();
         objDefaultLocale.Locale = "en-US";
         
         // Build the Checkout action
         CheckoutAction objCheckout = new CheckoutAction();
         objCheckout.reservationType = ReservationType.Exclusive;
         objCheckout.reservationTypeSpecified = true;
         
         // Assign the action to the ChangeRequestType element
         ChangeRequestType objChangeRequestType = new ChangeRequestType();
         objChangeRequestType.Action = new ActionType[1];
         objChangeRequestType.Action[0] = (ActionType)objCheckout;
         
         // Set a reference to the document to check out
         ObjectSpecification objDocument = new ObjectSpecification();
         objDocument.classId = "Document";
         objDocument.path = strDocPath;
         objDocument.objectStore = strObjStoreName;
   
         // Specify the target object (a document) for the actions
         objChangeRequestType.TargetSpecification = objDocument;
         objChangeRequestType.id = "1";
         
         // Create a property filter to get Reservation property
         PropertyFilterType objPropFilter = new PropertyFilterType();
         objPropFilter.maxRecursion = 5;
         objPropFilter.maxRecursionSpecified = true;
         
         // Create filter element array to hold IncludeProperties specifications
         objPropFilter.IncludeProperties = new FilterElementType[5];
         
         // Create filter element for CurrentVersion property
         objPropFilter.IncludeProperties[0] = new FilterElementType();
         objPropFilter.IncludeProperties[0].Value = "CurrentVersion";
         
         // Create filter element for Reservation property
         objPropFilter.IncludeProperties[1] = new FilterElementType();
         objPropFilter.IncludeProperties[1].Value = "Reservation";
         
         // Create filter element for ContentElements property
         objPropFilter.IncludeProperties[2] = new FilterElementType();
         objPropFilter.IncludeProperties[2].Value = "ContentElements";
         
         // Create filter element for Content pseudo-property
         objPropFilter.IncludeProperties[3] = new FilterElementType();
         objPropFilter.IncludeProperties[3].Value = "Content";
         objChangeRequestType.RefreshFilter= objPropFilter;
         
         // Create array of ChangeRequestType elements and assign ChangeRequestType element to it 
         ChangeRequestType[] objChangeRequestTypeArray = new ChangeRequestType[1];
         objChangeRequestTypeArray[0] = objChangeRequestType;
         
         // Create ChangeResponseType element array 
         ChangeResponseType[] objChangeResponseTypeArray;
   
         // Build ExecuteChangesRequest element and assign ChangeRequestType element array to it
         ExecuteChangesRequest objExecuteChangesRequest = new ExecuteChangesRequest(); 
         objExecuteChangesRequest.ChangeRequest = objChangeRequestTypeArray;
         objExecuteChangesRequest.refresh = true; // return a refreshed object
         objExecuteChangesRequest.refreshSpecified = true;
   
         try
         {
            // Call ExecuteChanges operation to implement the document checkout
            objChangeResponseTypeArray = wseService.ExecuteChanges(objExecuteChangesRequest);
         }
         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 (objChangeResponseTypeArray == null || objChangeResponseTypeArray.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 objChangeResponseTypeArray[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 objChangeResponseTypeArray[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 objChangeResponseTypeArray[0].Property)
         {
            if (prpProperty.propertyId == "ContentElements")
            {
               prpContentElements = (ListOfObject)prpProperty;
            }
         }
         
         // Get the content from each content element
         int intElementCount = (prpContentElements.Value == null) ? 0 : prpContentElements.Value.Length;
         for (int i = 0; i < intElementCount; 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 objInlineContent = prpContent.Value as InlineContent;
            
            // Write inline content data to file
            Stream outputStream = File.OpenWrite("saveContentPath");
            outputStream.Write(objInlineContent.Binary, 0, objInlineContent.Binary.Length);
            outputStream.Close();
         }

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

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

         // Build a list of properties to set in the new document
         ModifiablePropertyType[] objInputProps = new ModifiablePropertyType[2];
         
         // Specify and set a string-valued property for the DocumentTitle property
         SingletonString prpDocumentTitle = new SingletonString();
         prpDocumentTitle.Value = "out";
         prpDocumentTitle.propertyId = "DocumentTitle"; 
         objInputProps[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 objInlineContentNew = new InlineContent();
         System.IO.Stream inputStream = System.IO.File.OpenRead(strSaveContentPath);
         objInlineContentNew.Binary = new byte[inputStream.Length];
         inputStream.Read(objInlineContentNew.Binary, 0, (int)inputStream.Length);
         inputStream.Close();
   
         // Create reference to Content pseudo-property
         ContentData prpContentUpdate = new ContentData();
         prpContentUpdate.Value = (ContentType)objInlineContentNew;
         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;
         objInputProps[1] = prpContentElementsNew;
         
         // Assign list of document properties to set in ChangeRequestType element
         objUpdateChangeRequestType.ActionProperties = objInputProps;

         // Create array of ChangeRequestType elements and assign ChangeRequestType element to it 
         ChangeRequestType[] objUpdateChangeRequestTypeArray = new ChangeRequestType[1];
         objUpdateChangeRequestTypeArray[0] = objUpdateChangeRequestType;
         ChangeResponseType[] objUpdateChangeResponseTypeArray;
         
         // Create ChangeResponseType element array 
         ExecuteChangesRequest objUpdateExecuteChangesRequest = new ExecuteChangesRequest(); 
         objUpdateExecuteChangesRequest.ChangeRequest = objUpdateChangeRequestTypeArray;
         objUpdateExecuteChangesRequest.refresh = true; // return a refreshed object
         objUpdateExecuteChangesRequest.refreshSpecified = true;
   
         try
         {
            // Call ExecuteChanges operation to implement the document Update action
            objUpdateChangeResponseTypeArray = wseService.ExecuteChanges(objUpdateExecuteChangesRequest);
         }
         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 (objUpdateChangeResponseTypeArray==null || objUpdateChangeResponseTypeArray.Length < 1)
         {
            Console.WriteLine("A valid Document object was not returned from the ExecuteChanges operation");
            return;
         }
         Console.WriteLine("Content uploaded to document '{0}'.", strDocPath);
      
         // **********************************
         // Check in the document. 
         // **********************************

         // Build the Checkin action
         CheckinAction objCheckin = new CheckinAction();
            
         // Assign the action to the ChangeRequestType element
         ChangeRequestType objCheckinChangeRequestType = new ChangeRequestType();
         objCheckinChangeRequestType.Action = new ActionType[1];
         objCheckinChangeRequestType.Action[0] = (ActionType)objCheckin;
   
         // Specify the target object (Reservation object) for the actions
         objCheckinChangeRequestType.TargetSpecification = new ObjectReference(); 
         objCheckinChangeRequestType.TargetSpecification.classId = "Document";
         objCheckinChangeRequestType.TargetSpecification.objectId = objReservation.objectId; 
         objCheckinChangeRequestType.TargetSpecification.objectStore = strObjStoreName;
         objCheckinChangeRequestType.id = "1";
            
         // Assign ChangeRequestType element
         ChangeRequestType[] objCheckinChangeRequestTypeArray = new ChangeRequestType[1];
         objCheckinChangeRequestTypeArray[0] = objCheckinChangeRequestType;
         ChangeResponseType[] objCheckinChangeResponseTypeArray;
            
         // Build ExecuteChangesRequest element and assign ChangeRequestType element array to it
         ExecuteChangesRequest objCheckinExecuteChangesRequest = new ExecuteChangesRequest(); 
         objCheckinExecuteChangesRequest.ChangeRequest = objCheckinChangeRequestTypeArray;
         objCheckinExecuteChangesRequest.refresh = true; // return a refreshed object
         objCheckinExecuteChangesRequest.refreshSpecified = true;
   
         try
         {
            // Call ExecuteChanges operation to implement the doc checkin
            objCheckinChangeResponseTypeArray = wseService.ExecuteChanges(objCheckinExecuteChangesRequest);
         }
         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 (objCheckinChangeResponseTypeArray == null || objCheckinChangeResponseTypeArray.Length < 1)
         {
            Console.WriteLine("A valid Document object was not returned from the ExecuteChanges operation");
            return;
         }
         Console.WriteLine("Checked-in Document '{0}'.", strDocPath);
         Console.WriteLine("Press Enter to end");
         Console.ReadLine();   
      }
   }
}