Document Copy Example

The following code example demonstrates how to copy a document and its annotations. 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 documentcopy_ex.CEWS_SOAP;

namespace documentcopy_ex
{
   /// <summary>
   /// Copies a document's content and its annotations.
   /// </summary>
   class Class1
   {
      /// <summary>
      /// The main entry point for the application
      /// </summary>
      [STAThread]
      static void Main(string[] args)
      {
         // *********************************************************************
         // This code example retrieves a document from the object store and      
         // copies the content data of its content element and annotation  
         // content element to local files. In addition, the document's  
         // content is copied to a new document in the object store.
         // *********************************************************************
   
         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 object store
         const string DocContentPath = "c:\\Test\\ContentSave.doc";  // Path to a file for saving document content
         const string annotationContPath = "c:\\Test\\Annot.txt";  // Path to a file for saving annotation content
         string strID = "";
   
         // 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";

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

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

         ObjectResponseType[] elemObjectResponseTypeArray;
         try
         {
            elemObjectResponseTypeArray = wseService.GetObjects(elemObjectRequestTypeArray);
         }
         catch(System.Net.WebException ex)
         {
            Console.WriteLine("An exception occurred while requesting a document: [" + ex.Message + "]");
            return;
         }
         
         ObjectValue objDocument = null;
         if (elemObjectResponseTypeArray[0] is SingleObjectResponse)
         {
            objDocument = ((SingleObjectResponse)elemObjectResponseTypeArray[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 elementCount = (prpContentElements.Value == null) ? 0 : prpContentElements.Value.Length;
         for (int i = 0; i < elementCount; 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 elemInlineContentNew = prpContent.Value as InlineContent;
            
            // Write inline content to file
            Stream outputStream = File.OpenWrite(DocContentPath);
            outputStream.Write(elemInlineContentNew.Binary, 0, elemInlineContentNew.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 elemInlineContentAnnot = prpContentAnnot.Value as InlineContent;
            
               // Write inline content to file
               Stream outputStream = File.OpenWrite(annotationContPath);
               outputStream.Write(elemInlineContentAnnot.Binary, 0, elemInlineContentAnnot.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 verbCreate = new CreateAction();
         verbCreate.classId = "Document";          
         
         // Build the Checkin action
         CheckinAction verbCheckin = new CheckinAction();
         verbCheckin.checkinMinorVersion = true;
         verbCheckin.checkinMinorVersionSpecified = true;
   
         // Assign the actions to the ChangeRequestType element
         ChangeRequestType elemChangeRequestType = new ChangeRequestType();
         elemChangeRequestType.Action = new ActionType[2];
         elemChangeRequestType.Action[0] = (ActionType)verbCreate;
         elemChangeRequestType.Action[1] = (ActionType)verbCheckin;
   
         // Specify the target object (an object store) for the actions
         elemChangeRequestType.TargetSpecification = new ObjectReference(); 
         elemChangeRequestType.TargetSpecification.classId = "ObjectStore";
         elemChangeRequestType.TargetSpecification.objectId = objectStore; 
         elemChangeRequestType.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 elemInlineContent = new InlineContent();
         System.IO.Stream inputStream = System.IO.File.OpenRead(DocContentPath);
         elemInlineContent.Binary = new byte[inputStream.Length];
         inputStream.Read(elemInlineContent.Binary, 0, (int)inputStream.Length);
         inputStream.Close();
   
         // Create reference to Content pseudo-property
         ContentData prpContentDoc = new ContentData();
         prpContentDoc.Value = (ContentType)elemInlineContent;
         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;       
         elemChangeRequestType.ActionProperties = docInputProps;   
         
         // Assign list of properties to exclude
         string[] excludeProps = new string[2];
         excludeProps[0] = "Owner";
         excludeProps[1] = "DateLastModified";     
         elemChangeRequestType.RefreshFilter = new PropertyFilterType();
         elemChangeRequestType.RefreshFilter.ExcludeProperties = excludeProps;  
         
         // Create ChangeRequestType element array
         ChangeRequestType[] elemChangeRequestTypeArray = new ChangeRequestType[1];
         elemChangeRequestTypeArray[0] = elemChangeRequestType;        
         
         // Create ChangeResponseType element array 
         ChangeResponseType[] elemChangeResponseTypeArray = null;
   
         // Build ExecuteChangesRequest element
         ExecuteChangesRequest elemExecuteChangesRequest = new ExecuteChangesRequest(); 
         elemExecuteChangesRequest.ChangeRequest = elemChangeRequestTypeArray;
         elemExecuteChangesRequest.refresh = true;  // return a refreshed object
         elemExecuteChangesRequest.refreshSpecified = true;
   
         try
         {
            // Call ExecuteChanges operation to implement document creation and check in
            elemChangeResponseTypeArray = wseService.ExecuteChanges(elemExecuteChangesRequest);
         }
         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 (elemChangeResponseTypeArray == null || elemChangeResponseTypeArray.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 elemChangeResponseTypeArray[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 elemChangeRequestTypeAnnot = new ChangeRequestType();
         elemChangeRequestTypeAnnot.Action = new ActionType[1];
         elemChangeRequestTypeAnnot.Action[0] = (ActionType)annotationCreate;
   
         // Specify the target object (an object store) for the actions
         elemChangeRequestTypeAnnot.TargetSpecification = new ObjectReference(); 
         elemChangeRequestTypeAnnot.TargetSpecification.classId = "ObjectStore";
         elemChangeRequestTypeAnnot.TargetSpecification.objectId = objectStore; 
         elemChangeRequestTypeAnnot.id = "1"; 

         // Specify the document for annotatedObject
         ObjectReference objDocumentAnnot = new ObjectReference();
         objDocumentAnnot.classId = "Document";
         objDocumentAnnot.objectStore = objectStore;
         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 elemInlineContentAnnotNew = new InlineContent();
         System.IO.Stream inputStreamAnnot = System.IO.File.OpenRead(annotationContPath);
         elemInlineContentAnnotNew.Binary = new byte[inputStreamAnnot.Length];
         inputStreamAnnot.Read(elemInlineContentAnnotNew.Binary, 0, (int)inputStreamAnnot.Length);
         inputStreamAnnot.Close();
   
         // Create reference to Content pseudo-property
         ContentData prpContentAnnotNew = new ContentData();
         prpContentAnnotNew.Value = (ContentType)elemInlineContentAnnotNew;
         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;         
         elemChangeRequestTypeAnnot.ActionProperties = inputPropsAnnot;
         
         // Assign list of properties to exclude
         string[] excludePropsAnnot = new string[2];
         excludePropsAnnot[0] = "Owner";
         excludePropsAnnot[1] = "DateLastModified"; 
         elemChangeRequestTypeAnnot.RefreshFilter = new PropertyFilterType();
         elemChangeRequestTypeAnnot.RefreshFilter.ExcludeProperties = excludePropsAnnot; 
         
         // Create ChangeRequestType element array         
         ChangeRequestType[] elemChangeRequestTypeArrayAnnot = new ChangeRequestType[1];
         elemChangeRequestTypeArrayAnnot[0] = elemChangeRequestTypeAnnot; 
         
         // Create ChangeResponseType element array         
         ChangeResponseType[] elemChangeResponseTypeArrayAnnot = null;
         
         // Build ExecuteChangesRequest element                  
         ExecuteChangesRequest elemExecuteChangesRequestAnnot = new ExecuteChangesRequest(); 
         elemExecuteChangesRequestAnnot.ChangeRequest = elemChangeRequestTypeArrayAnnot;
         elemExecuteChangesRequestAnnot.refresh = true; // return a refreshed object
         elemExecuteChangesRequestAnnot.refreshSpecified = true;
         
         try
         {
            // Call ExecuteChanges operation to implement the Annotation creation and checkin
            elemChangeResponseTypeArrayAnnot = wseService.ExecuteChanges(elemExecuteChangesRequestAnnot);
         }
         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 (elemChangeResponseTypeArrayAnnot == null || elemChangeResponseTypeArrayAnnot.Length < 1)
         {
            Console.WriteLine("A valid Annotation object was not returned from the ExecuteChanges operation");
            return;
         }  
                                  
         Console.WriteLine("New Annotation object has been created");
      }
   }
}