Document Checkout Example

The following code example illustrates how to check out an existing document and check it back 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.Security.Tokens;
using documentcheckout_ex.CEWS_DIME;


namespace documentcheckout_ex
   {
   /// <summary>
   /// Document check out and check in.
   /// </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 objectStore = "ContentEngineExs"; // Pre-existing object store
         const string documentPath = "/Test/DemoDoc";   // Path to a pre-existing document in object store
   
         // 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 = 1;
         elemPropFilter.maxRecursionSpecified = true;
         elemPropFilter.IncludeProperties = new FilterElementType[1];
         elemPropFilter.IncludeProperties[0] = new FilterElementType();
         elemPropFilter.IncludeProperties[0].Value = "Reservation";
		 
         // Assign the list of included properties to the ChangeRequestType element
         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 doc checkout
            elemChangeResponseTypeArray = wseService.ExecuteChanges(elemExecuteChangesRequest);
         }
         catch(System.Net.WebException ex)
         {
            Console.WriteLine("An exception occurred while checking in 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;
         }
   
         // Get Reservation object from Reservation property
         SingletonObject propReservation = (SingletonObject) elemChangeResponseTypeArray[0].Property[0];
         ObjectValue objReservation = (ObjectValue) propReservation.Value;
		 
         // Build the Checkin action
         CheckinAction verbCheckin = new CheckinAction();
		 
         // Assign the action to the ChangeRequestType element
         elemChangeRequestType.Action = new ActionType[1];
         elemChangeRequestType.Action[0] = (ActionType)verbCheckin;
   
         // Specify the target object (Reservation object) for the actions
         elemChangeRequestType.TargetSpecification = new ObjectReference(); 
         elemChangeRequestType.TargetSpecification.classId = "Document";
         elemChangeRequestType.TargetSpecification.objectId = objReservation.objectId; 
         elemChangeRequestType.TargetSpecification.objectStore = objectStore;
         elemChangeRequestType.id = "1";
		 
         // Assign ChangeRequestType element
         elemChangeRequestTypeArray[0] = elemChangeRequestType;
   
         // Build ExecuteChangesRequest element and assign ChangeRequestType element array to it
         elemExecuteChangesRequest.ChangeRequest = elemChangeRequestTypeArray;
         elemExecuteChangesRequest.refresh = true; // return a refreshed object
         elemExecuteChangesRequest.refreshSpecified = true;
   
         try
         {
            // Call ExecuteChanges operation to implement the doc checkout
            elemChangeResponseTypeArray = wseService.ExecuteChanges(elemExecuteChangesRequest);
         }
         catch(System.Net.WebException ex)
         {
            Console.WriteLine("An exception occurred while checking out 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;
         }
		 
         Console.WriteLine("Checked-in Document '{0}'.", documentPath);
		 
      } //Main
   } // class1
}