Document Copy Example

The following code example demonstrates how to copy the contents of a document's content element and its annotation content element to a new 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 documentcopy_ex.CEWS;

namespace documentcopy_ex
{
   /// <summary>
   /// Copies a the contents of a document's content element and its annotation content element to local files, 
   /// then copies the content to a new document. The document must have a content element and an annotation with 
   /// a content element.
   /// </summary>
   class Class1
   {
      static void Main(string[] args)
      {
         const string strUrl = "http://localhost:9080/wsi/FNCEWS40SOAP/"; // Change port number if necessary
         const string strObjectStoreName = "ContentEngineExs";  // Pre-existing object store
         const string strDocPath = "/TestFolder/DemoDoc";  // Path to pre-existing document in object store
         const string strDocContentPath = "c:\\Test\\ContentSave.doc";  // Path to a file for saving document content
         const string strAnnotContentPath = "c:\\Test\\Annot.txt";  // Path to a file for saving annotation content
         string strID = "";
   
         // 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";

         // Set a reference to the document to retrieve
         ObjectSpecification objDocumentSpec = new ObjectSpecification();
         objDocumentSpec.classId = "Document";
         objDocumentSpec.path = strDocPath;
         objDocumentSpec.objectStore = strObjectStoreName;
         
         // *******************************************************************
         // Create a property filter to get document and annotation content.
         // *******************************************************************
         
         // Create property filter object and set its attributes
         PropertyFilterType objPropFilter = new PropertyFilterType();
         objPropFilter.maxRecursion = 4;
         objPropFilter.maxRecursionSpecified = true;
         
         // Create filter element array to hold IncludeProperties specifications
         objPropFilter.IncludeProperties = new FilterElementType[4];
         
         // Create filter element for Annotations property
         objPropFilter.IncludeProperties[0] = new FilterElementType();
         objPropFilter.IncludeProperties[0].Value = "Annotations";
         
         // Create filter element for ContentElements property
         objPropFilter.IncludeProperties[1] = new FilterElementType();
         objPropFilter.IncludeProperties[1].Value = "ContentElements";
         
         // Create filter element for Content pseudo-property
         objPropFilter.IncludeProperties[2] = new FilterElementType();
         objPropFilter.IncludeProperties[2].Value = "Content";

         // ********************************************************
         // Create an object request for a GetObjects operation.
         // ********************************************************
         
         ObjectRequestType[] objObjectRequestTypeArray = new ObjectRequestType[1];
         objObjectRequestTypeArray[0] = new ObjectRequestType();
         objObjectRequestTypeArray[0].SourceSpecification = objDocumentSpec;
         objObjectRequestTypeArray[0].PropertyFilter = objPropFilter;

         // Call GetObjects operation to get document object and its properties
         ObjectResponseType[] objObjectResponseTypeArray;
         try
         {
            objObjectResponseTypeArray = wseService.GetObjects(objObjectRequestTypeArray);
         }
         catch(System.Net.WebException ex)
         {
            Console.WriteLine("An exception occurred while requesting a document: [" + ex.Message + "]");
            return;
         }
         
         ObjectValue objDocument = null;
         if (objObjectResponseTypeArray[0] is SingleObjectResponse)
         {
            objDocument = ((SingleObjectResponse)objObjectResponseTypeArray[0]).Object;
         }

         // *****************************************************************************
         // Retrieve the content data of a content element and write to a local file.
         // *****************************************************************************
                           
         // Get the ContentElements property of the Document object
         ListOfObject prpContentElements = null;
         foreach (PropertyType prpProperty in objDocument.Property)
         {
            if (prpProperty.propertyId == "ContentElements")
            {
               prpContentElements = (ListOfObject)prpProperty;
            }
         }
         
         int intElementCount = (prpContentElements.Value == null) ? 0 : prpContentElements.Value.Length;
         for (int i = 0; i < intElementCount; i++)
         {
            // Get a ContentTransfer object from 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 objInlineContentNew = prpContent.Value as InlineContent;
            
            // Write inline content to file
            Stream outputStream = File.OpenWrite(strDocContentPath);
            outputStream.Write(objInlineContentNew.Binary, 0, objInlineContentNew.Binary.Length);
            outputStream.Close();
            Console.WriteLine("Document content has been written");            
         }
         
         // *******************************************************
         // Retrieve annotation content and write to local file.
         // *******************************************************

         // Get Annotations property from the Document object
         EnumOfObject prpAnnotations = null;
         foreach (PropertyType prpProperty in objDocument.Property)
         {
             if (prpProperty.propertyId == "Annotations")
             {
                prpAnnotations = (EnumOfObject)prpProperty;
             }
         }

         ListOfObject prpContentElementsAnnot = null;
         int annotationCount = (prpAnnotations.Value == null) ? 0 : prpAnnotations.Value.Length;
         for (int i = 0; i < annotationCount; i++)
         {
            // Get the ContentElements property of each Annotation object
            foreach (PropertyType prpProperty in prpAnnotations.Value[i].Property)
            {
               if (prpProperty.propertyId == "ContentElements")
               {
                  prpContentElementsAnnot = (ListOfObject)prpProperty;
                }
            }

            int annotElementCount = (prpContentElementsAnnot.Value == null) ? 0 : prpContentElementsAnnot.Value.Length;
            for (int j = 0; j < annotElementCount; j++)
            {
               // Get a ContentTransfer object from the ContentElements property collection
               DependentObjectType objContentTransferAnnot = prpContentElementsAnnot.Value[j];
               
                // Get Content pseudo-property from the ContentTransfer object
                ContentData prpContentAnnot = null;
                foreach (PropertyType prpProperty in objContentTransferAnnot.Property)
                {
                   if (prpProperty.propertyId == "Content")
                   {
                     prpContentAnnot = (ContentData)prpProperty;
                   }
                }

               // Get inline content data from Content property value
               InlineContent objInlineContentAnnot = prpContentAnnot.Value as InlineContent;
            
               // Write inline content to file
               Stream outputStream = File.OpenWrite(strAnnotContentPath);
               outputStream.Write(objInlineContentAnnot.Binary, 0, objInlineContentAnnot.Binary.Length);
               outputStream.Close();
               Console.WriteLine("Annotation content has been written");
            }
         }

         // ****************************************
         // Create a document with content.
         // ****************************************
         
         // Build the Create action for a new document
         CreateAction objCreate = new CreateAction();
         objCreate.classId = "Document";          
         
         // Build the Checkin action
         CheckinAction objCheckin = new CheckinAction();
         objCheckin.checkinMinorVersion = true;
         objCheckin.checkinMinorVersionSpecified = true;
   
         // Assign the actions to the ChangeRequestType element
         ChangeRequestType objChangeRequestType = new ChangeRequestType();
         objChangeRequestType.Action = new ActionType[2];
         objChangeRequestType.Action[0] = (ActionType)objCreate;
         objChangeRequestType.Action[1] = (ActionType)objCheckin;
   
         // Specify the target object (an object store) for the actions
         objChangeRequestType.TargetSpecification = new ObjectReference(); 
         objChangeRequestType.TargetSpecification.classId = "ObjectStore";
         objChangeRequestType.TargetSpecification.objectId = strObjectStoreName; 
         objChangeRequestType.id = "1";         
            
         // Build a list of properties to set in the new doc
         ModifiablePropertyType[] docInputProps = new ModifiablePropertyType[2];     
             
         // Specify and set a string-valued property for the DocumentTitle property
         SingletonString prpDocumentTitle = new SingletonString();
         prpDocumentTitle.Value = "NewDocument";
         prpDocumentTitle.propertyId = "DocumentTitle"; 
         docInputProps[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 objInlineContent = new InlineContent();
         System.IO.Stream inputStream = System.IO.File.OpenRead(strDocContentPath);
         objInlineContent.Binary = new byte[inputStream.Length];
         inputStream.Read(objInlineContent.Binary, 0, (int)inputStream.Length);
         inputStream.Close();
   
         // Create reference to Content pseudo-property
         ContentData prpContentDoc = new ContentData();
         prpContentDoc.Value = (ContentType)objInlineContent;
         prpContentDoc.propertyId = "Content";         
         
         // Assign Content property to ContentTransfer object 
         objContentTransferNew.Property[0] = prpContentDoc;  
         
         // Create and assign ContentType 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;
         
         // Assign list of properties to set
         docInputProps[1] = prpContentElementsNew;       
         objChangeRequestType.ActionProperties = docInputProps;   
         
         // Assign list of properties to exclude
         string[] excludeProps = new string[2];
         excludeProps[0] = "Owner";
         excludeProps[1] = "DateLastModified";     
         objChangeRequestType.RefreshFilter = new PropertyFilterType();
         objChangeRequestType.RefreshFilter.ExcludeProperties = excludeProps;  
         
         // Create ChangeRequestType element array
         ChangeRequestType[] objChangeRequestTypeArray = new ChangeRequestType[1];
         objChangeRequestTypeArray[0] = objChangeRequestType;        
         
         // Create ChangeResponseType element array 
         ChangeResponseType[] objChangeResponseTypeArray = null;
   
         // Build ExecuteChangesRequest element
         ExecuteChangesRequest objExecuteChangesRequest = new ExecuteChangesRequest(); 
         objExecuteChangesRequest.ChangeRequest = objChangeRequestTypeArray;
         objExecuteChangesRequest.refresh = true;  // return a refreshed object
         objExecuteChangesRequest.refreshSpecified = true;
   
         try
         {
            // Call ExecuteChanges operation to implement document creation and check in
            objChangeResponseTypeArray = wseService.ExecuteChanges(objExecuteChangesRequest);
         }
         catch(System.Net.WebException ex)
         {
            Console.WriteLine("An exception occurred while creating a document: [" + ex.Message + "]");
            return;
         }    
              
         // Return the new 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;
         }         
         
         Console.WriteLine("New Document object has been created");         
         
         // Capture value of the DocumentTitle property in the returned doc object
         foreach (PropertyType prpProperty in objChangeResponseTypeArray[0].Property)
         {
            // If property found, store its value
            if (prpProperty.propertyId == "Id")
            {
               strID = ((SingletonId)prpProperty).Value;
               break;
            }
         }         

         // ****************************************
         // Create an annotation with content.
         // ****************************************
         
         // Build the Create action for a new annotation 
         CreateAction annotationCreate = new CreateAction();
         annotationCreate.classId = "Annotation"; 
   
         // Assign the action to the ChangeRequestType element
         ChangeRequestType objChangeRequestTypeAnnot = new ChangeRequestType();
         objChangeRequestTypeAnnot.Action = new ActionType[1];
         objChangeRequestTypeAnnot.Action[0] = (ActionType)annotationCreate;
   
         // Specify the target object (an object store) for the actions
         objChangeRequestTypeAnnot.TargetSpecification = new ObjectReference(); 
         objChangeRequestTypeAnnot.TargetSpecification.classId = "ObjectStore";
         objChangeRequestTypeAnnot.TargetSpecification.objectId = strObjectStoreName; 
         objChangeRequestTypeAnnot.id = "1"; 

         // Specify the document for annotatedObject
         ObjectReference objDocumentAnnot = new ObjectReference();
         objDocumentAnnot.classId = "Document";
         objDocumentAnnot.objectStore = strObjectStoreName;
         objDocumentAnnot.objectId = strID;

         // Specify annotation properties to set in the new doc
         ModifiablePropertyType[] inputPropsAnnot = new ModifiablePropertyType[2];
         
         // Create the AnnotatedObject property
         SingletonObject AnnotatedObject = new SingletonObject();
         AnnotatedObject.propertyId = "AnnotatedObject";
         AnnotatedObject.Value = (ObjectEntryType)objDocumentAnnot;
         inputPropsAnnot[0] = AnnotatedObject;
         
         // Create annotation content
         DependentObjectType objContentTransferAnnotNew = new DependentObjectType();
         objContentTransferAnnotNew.classId = "ContentTransfer";
         objContentTransferAnnotNew.dependentAction = DependentObjectTypeDependentAction.Insert;
         objContentTransferAnnotNew.dependentActionSpecified = true;
         objContentTransferAnnotNew.Property = new PropertyType[2];         
            
         // Create reference to the ContentTransfer objects returned by the ContentElements property
         ListOfObject prpContentElementsAnnotNew = new ListOfObject();
         prpContentElementsAnnotNew.propertyId = "ContentElements";
         prpContentElementsAnnotNew.Value = new DependentObjectType[1];
         prpContentElementsAnnotNew.Value[0] = objContentTransferAnnotNew;          
            
         // Read data stream from file containing the annotation content
         InlineContent objInlineContentAnnotNew = new InlineContent();
         System.IO.Stream inputStreamAnnot = System.IO.File.OpenRead(strAnnotContentPath);
         objInlineContentAnnotNew.Binary = new byte[inputStreamAnnot.Length];
         inputStreamAnnot.Read(objInlineContentAnnotNew.Binary, 0, (int)inputStreamAnnot.Length);
         inputStreamAnnot.Close();
   
         // Create reference to Content pseudo-property
         ContentData prpContentAnnotNew = new ContentData();
         prpContentAnnotNew.Value = (ContentType)objInlineContentAnnotNew;
         prpContentAnnotNew.propertyId = "Content"; 
         
         // Assign Content property to ContentTransfer object 
         objContentTransferAnnotNew.Property[0] = prpContentAnnotNew;     
         
         // Create and assign ContentType property to ContentTransfer object            
         SingletonString prpContentTypeAnnot = new SingletonString();
         prpContentTypeAnnot.propertyId = "ContentType";
         prpContentTypeAnnot.Value = "application/msword"; 
         objContentTransferAnnotNew.Property[1] = prpContentTypeAnnot;
         
         // Assign list of properties to set
         inputPropsAnnot[1] = prpContentElementsAnnotNew;         
         objChangeRequestTypeAnnot.ActionProperties = inputPropsAnnot;
         
         // Assign list of properties to exclude
         string[] excludePropsAnnot = new string[2];
         excludePropsAnnot[0] = "Owner";
         excludePropsAnnot[1] = "DateLastModified"; 
         objChangeRequestTypeAnnot.RefreshFilter = new PropertyFilterType();
         objChangeRequestTypeAnnot.RefreshFilter.ExcludeProperties = excludePropsAnnot; 
         
         // Create ChangeRequestType element array         
         ChangeRequestType[] objChangeRequestTypeArrayAnnot = new ChangeRequestType[1];
         objChangeRequestTypeArrayAnnot[0] = objChangeRequestTypeAnnot; 
         
         // Create ChangeResponseType element array         
         ChangeResponseType[] objChangeResponseTypeArrayAnnot = null;
         
         // Build ExecuteChangesRequest element                  
         ExecuteChangesRequest objExecuteChangesRequestAnnot = new ExecuteChangesRequest(); 
         objExecuteChangesRequestAnnot.ChangeRequest = objChangeRequestTypeArrayAnnot;
         objExecuteChangesRequestAnnot.refresh = true; // return a refreshed object
         objExecuteChangesRequestAnnot.refreshSpecified = true;
         
         try
         {
            // Call ExecuteChanges operation to implement the Annotation creation and checkin
            objChangeResponseTypeArrayAnnot = wseService.ExecuteChanges(objExecuteChangesRequestAnnot);
         }
         catch(System.Net.WebException ex)
         {
            Console.WriteLine("An exception occurred while creating a Annotation: [" + ex.Message + "]");
            return;
         }   
         
         // Return the new Annotation object, unless there is an error
         if (objChangeResponseTypeArrayAnnot == null || objChangeResponseTypeArrayAnnot.Length < 1)
         {
            Console.WriteLine("A valid Annotation object was not returned from the ExecuteChanges operation");
            return;
         }  
                                  
         Console.WriteLine("New Annotation object has been created");
         Console.WriteLine("Press Enter to end");
         Console.ReadLine();   
      }
   }
}