Document Creation DIME Content Example

The following code example illustrates how to create a document, add content to it using a DIME attachment, and check it in. This example assumes a web reference named CEWS_DIME has been set to the following URI: http://localhost:<port>/wsi/FNCEWS35DIME/wsdl.

using System;
using Microsoft.Web.Services2;
using Microsoft.Web.Services2.Dime;
using Microsoft.Web.Services2.Security.Tokens;
using documentcreatedime_ex.CEWS_DIME;

namespace documentcreatedime_ex
{
   /// <summary>
   /// Create and check in a document using DIME attachments.
   /// </summary>
   class Class1
   {
      /// <summary>
      /// The main entry point for the application.
      /// </summary>
      [STAThread]
      static void Main(string[] args)
      {
         const string url = "http://localhost:9080/wsi/FNCEWS35DIME/"; // Change port number if necessary
         const string contentFile = "c:/test/demo.doc";  // Path to a pre-existing document
         const string objectStore = "ContentEngineExs"; // Pre-existing object store
         string documentTitle = "DemoDoc"; // DocumentTitle property value of the new document
         System.DateTime dateCreated = new System.DateTime();
		 
         // 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 Create action for a 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;  // Assign Create action
         elemChangeRequestType.Action[1] = (ActionType)verbCheckin; // Assign Checkin action
   
         // 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[] elemInputProps = new ModifiablePropertyType[2];
		 
         // Specify and set a string-valued property for the DocumentTitle property
         SingletonString propDocumentTitle = new SingletonString();
         propDocumentTitle.Value = documentTitle;
         propDocumentTitle.propertyId = "DocumentTitle"; 
         elemInputProps[0] = propDocumentTitle; // Add to property list
		 
         // Create an object reference to dependently persistable ContentTransfer object
         DependentObjectType objContentTransfer = new DependentObjectType();
         objContentTransfer.classId = "ContentTransfer";
         objContentTransfer.dependentAction = DependentObjectTypeDependentAction.Insert;
         objContentTransfer.dependentActionSpecified = true;
         objContentTransfer.Property = new PropertyType[2];
		 
         // Create reference to the object set of ContentTransfer objects returned by the Document.ContentElements property
         ListOfObject propContentElement = new ListOfObject();
         propContentElement.propertyId = "ContentElements";
         propContentElement.Value = new DependentObjectType[1];
         propContentElement.Value[0] = objContentTransfer;
		 
         // Create DIME attachment and add to SOAP header
         DimeAttachment elemDimeAttach = new DimeAttachment("application/msword", TypeFormat.MediaType,contentFile);
         wseService.RequestSoapContext.Attachments.Add(elemDimeAttach);
		 
         // Set DimeContent element to DIME attachment ID
         DIMEContent elemDimeContent = new DIMEContent(); 
         elemDimeContent.Attachment = new DIMEAttachmentReference();
         elemDimeContent.Attachment.location = elemDimeAttach.Id;
   
         // Create reference to Content pseudo-property
         ContentData propContent = new ContentData();
         propContent.Value = (ContentType)elemDimeContent;
         propContent.propertyId = "Content";
		 
         // Assign Content property to ContentTransfer object 
         objContentTransfer.Property[0] = propContent;
		 
         // Create and assign ContentType string-valued property to ContentTransfer object
         SingletonString propContentType = new SingletonString();
         propContentType.propertyId = "ContentType";
         propContentType.Value = "application/msword"; // Set MIME-type to MS Word
         objContentTransfer.Property[1] = propContentType;
         elemInputProps[1] = propContentElement;
		 
         // Assign list of document properties to set in ChangeRequestType element
         elemChangeRequestType.ActionProperties = elemInputProps;
		 
         // Build a list of properties to exclude on the new doc object that will be returned
         string[] excludeProps = new string[2];
         excludeProps[0] = "Owner";
         excludeProps[1] = "DateLastModified";
		 
         // Assign the list of excluded properties to the ChangeRequestType element
         elemChangeRequestType.RefreshFilter = new PropertyFilterType();
         elemChangeRequestType.RefreshFilter.ExcludeProperties = excludeProps;
		 
         // 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 = null;
   
         // 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 doc creation and checkin
            elemChangeResponseTypeArray = wseService.ExecuteChanges(elemExecuteChangesRequest);
         }
         catch(System.Net.WebException ex)
         {
            Console.WriteLine("An exception occurred while creating a document: [" + ex.Message + "]");
            return;
         }
		 
         // The new document object should be returned, unless there is an error
         if (elemChangeResponseTypeArray == null || elemChangeResponseTypeArray.Length < 1)
         {
            Console.WriteLine("A valid object was not returned from the ExecuteChanges operation");
            return;
         }
		 
         // Capture value of the DocumentTitle property in the returned doc object
         foreach (PropertyType propProperty in elemChangeResponseTypeArray[0].Property)
         {
            // If property found, store its value
            if (propProperty.propertyId == "DocumentTitle")
            {
               documentTitle = ((SingletonString)propProperty).Value;
               break;
            }
         }
		 
         // Capture value of the DateCreated property in the returned doc object
         foreach (PropertyType propProperty in elemChangeResponseTypeArray[0].Property)
         {
            // If property found, store its value
            if (propProperty.propertyId == "DateCreated")
            {
               dateCreated = ((SingletonDateTime)propProperty).Value;
               break;
            }
         }
		 
         Console.WriteLine("The document " + documentTitle + " was successfully created " + dateCreated + ".");

      } // Main
   } // Class1
}