Document Checkin Example

The following code example demonstrates how to check in a document reservation. This example assumes a web reference named CEWS has been set to the following URI: http://localhost:<port>/wsi/FNCEWS40MTOM/wsdl.

using System;      
using Microsoft.Web.Services3;
using Microsoft.Web.Services3.Security.Tokens;
using Microsoft.Web.Services3.Design;
using documentcheckin_ex.CEWS;

namespace documentcheckin_ex
{
   /// <summary>
   /// Check in a document reservation.
   /// </summary>
   class Class1
   {
      static void Main(string[] args)
      {
         const string strUrl = "http://localhost:9080/wsi/FNCEWS40MTOM/"; // Change port number if necessary
         const string strObjStoreName = "ContentEngineExs"; // Pre-existing object store
         const string strDocPath = "/TestFolder/DemoDoc";   // Path to a checked-out document in the object store
       
         // 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;
         wseService.RequireMtom = true;
         
         // Specify a Document object to retrieve
         ObjectSpecification objObjectSpecification = new ObjectSpecification();
         objObjectSpecification.classId = "Document";  // Symbolic name of Document class
         objObjectSpecification.path = strDocPath;  // Path to the document
         objObjectSpecification.objectStore = strObjStoreName;  // Object store in which the document is located

         // Create a property filter to limit the properties returned from the Document object
         PropertyFilterType objPropFilter = new PropertyFilterType();
         objPropFilter.maxRecursion = 1;
         objPropFilter.maxRecursionSpecified = true;
         
         // Create filter element array to hold an IncludeProperties specification for the Reservation property
         objPropFilter.IncludeProperties = new FilterElementType[1];
         objPropFilter.IncludeProperties[0] = new FilterElementType();
         objPropFilter.IncludeProperties[0].Value = "Reservation";
         
         // Create the request for GetObjects
         ObjectRequestType[] objObjectRequestTypeArray = new ObjectRequestType[1];
         objObjectRequestTypeArray[0] = new ObjectRequestType();
         objObjectRequestTypeArray[0].SourceSpecification = objObjectSpecification;
         objObjectRequestTypeArray[0].PropertyFilter = objPropFilter;

         // Create an object response for a GetObjects operation
         ObjectResponseType[] objObjectResponseTypeArray;

         try
         {
            // Get the Document object from the server
            objObjectResponseTypeArray = wseService.GetObjects(objObjectRequestTypeArray);
         }
         catch(System.Net.WebException ex)
         {
            Console.WriteLine("An exception occurred while requesting a document: [" + ex.Message + "]");
            return;
         }
         
         // Create an ObjectValue element to hold the properties collection of the retrieved Document object         
         ObjectValue objDocument = null;
         if (objObjectResponseTypeArray[0] is SingleObjectResponse)
         {
            objDocument = ((SingleObjectResponse)objObjectResponseTypeArray[0]).Object;
         }

         // Get Document's Reservation property
         SingletonObject prpReservation = null;
         foreach (PropertyType prpProperty in objDocument.Property)
         {
            if (prpProperty.propertyId == "Reservation")
            {
               prpReservation = (SingletonObject)prpProperty;
            }
         }
		 
         // Verify that the document reservation exists
         if (prpReservation.Value == null)
         {
            Console.WriteLine("The document is not checked out");
            return;
         }

         // Get Reservation object from the Document's Reservation property
         ObjectValue objReservation = (ObjectValue)prpReservation.Value;
         
         // Build Checkin action
         CheckinAction objCheckin = new CheckinAction();
         
         // Assign the action to the ChangeRequestType element
         ChangeRequestType[] objChangeRequestTypeArray = new ChangeRequestType[1];
         ChangeRequestType objChangeRequestType = new ChangeRequestType();
         objChangeRequestType.Action = new ActionType[1];
         objChangeRequestType.Action[0] = (ActionType)objCheckin;
         objChangeRequestTypeArray[0] = objChangeRequestType;
         
         // Create ChangeResponseType element array 
         ChangeResponseType[] objChangeResponseTypeArray;
   
         // Build ExecuteChangesRequest element
         ExecuteChangesRequest objExecuteChangesRequest = new ExecuteChangesRequest(); 
         objExecuteChangesRequest.ChangeRequest = objChangeRequestTypeArray;
         objExecuteChangesRequest.refresh = true; // Return a refreshed object
         objExecuteChangesRequest.refreshSpecified = true;
   
         // Specify the target object (Reservation object) for the actions
         objChangeRequestType.TargetSpecification = new ObjectReference(); 
         objChangeRequestType.TargetSpecification.classId = "Document";
         objChangeRequestType.TargetSpecification.objectId = objReservation.objectId;  
         objChangeRequestType.TargetSpecification.objectStore = strObjStoreName;
         objChangeRequestType.id = "1";
         
         // Assign ChangeRequestType element
         objChangeRequestTypeArray[0] = objChangeRequestType;
   
         // Build ExecuteChangesRequest element
         objExecuteChangesRequest.ChangeRequest = objChangeRequestTypeArray;
         objExecuteChangesRequest.refresh = true;  // Return a refreshed object
         objExecuteChangesRequest.refreshSpecified = true;
   
         try
         {
            // Call ExecuteChanges operation to implement the Reservation object
            objChangeResponseTypeArray = wseService.ExecuteChanges(objExecuteChangesRequest);
         }
         catch(System.Net.WebException ex)
         {
            Console.WriteLine("An exception occurred while checking in 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 object was not returned from the ExecuteChanges operation");
            return;
         }
         Console.WriteLine("Checked-in Document '{0}'.", strDocPath);
         Console.WriteLine("Press Enter to end");
         Console.ReadLine();
      }
   }
}