Document Unfile Example

The following code example demonstrates how to unfile a document from a folder. 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 Microsoft.Web.Services2;
using Microsoft.Web.Services2.Dime;
using Microsoft.Web.Services2.Security.Tokens;
using unfiledocument_ex.CEWS_SOAP;

namespace unfiledocument_ex
{
	/// <summary>
	/// Unfile a document from a folder.
	/// </summary>
	class UnFileDocument
	{
		/// <summary>
		/// The main entry point for the application
		/// </summary>
		[STAThread]
		static void Main(string[] args)
		{
			//**************************************************************
			// This code example demonstrates how to unfile a document.
			//**************************************************************
			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 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";
			
			// Set a reference to the document
			ObjectSpecification objDocumentSpec = new ObjectSpecification();
			objDocumentSpec.classId = "Document";
			objDocumentSpec.path = documentPath;
			objDocumentSpec.objectStore = objectStore;

			// Create a property filter to get Containers 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 = "Containers";

			// Create the request for GetObjects
			ObjectRequestType[] request = new ObjectRequestType[1];
			request[0] = new ObjectRequestType();
			request[0].SourceSpecification =objDocumentSpec;
			request[0].PropertyFilter = elemPropFilter;

			ObjectResponseType[] response;
			try
			{
				response = wseService.GetObjects(request);
			}
			catch(System.Net.WebException ex)
			{
				Console.WriteLine("An exception occurred while requesting a document: [" + ex.Message + "]");
				return;
			}
			
			ObjectValue objResponse = null;
			if (response[0] is SingleObjectResponse)
			{
				objResponse = ((SingleObjectResponse)response[0]).Object;
			}

			// Get ReferentialContainmentRelationship object
			EnumOfObject objRes=(EnumOfObject)objResponse.Property.GetValue(0);
			ObjectValue objRCR=(ObjectValue)objRes.Value[0]; // unfile from first folder (assume document is only filed in one folder)
			
			// Create delete action
			DeleteAction docUnfile = new DeleteAction ();
			
			// Assign the action to the ChangeRequestType element
			ChangeRequestType[] elemChangeRequestTypeArray = new ChangeRequestType[1];
			ChangeRequestType elemChangeRequestType = new ChangeRequestType();
			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;
			
			elemChangeRequestType.Action = new ActionType[1];
			elemChangeRequestType.Action[0] = (ActionType)docUnfile;
   
			// Specify the target object (Reservation object) for the actions
			elemChangeRequestType.TargetSpecification = new ObjectReference(); 
			elemChangeRequestType.TargetSpecification.classId = "ReferentialContainmentRelationship";
			elemChangeRequestType.TargetSpecification.objectId =objRCR.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 Delete object
				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 will 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("UnFile Document '{0}'.", documentPath);
		}
	}
}