The following code example illustrates how to create a document, add content to it using MTOM, and check it in. 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 documentcreatemtom_ex.CEWS; namespace documentcreatemtom_ex { /// <summary> /// Create and check in a document using MTOM. /// </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 strContentFilePath = "c:/test/demo.doc"; // Path to a pre-existing file containing the content string strDocTitle = "DemoDocMTOM"; // DocumentTitle property value of the new document System.DateTime dateCreated = new System.DateTime(); // 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; // Use MTOM to handle content data wseService.RequireMtom = true; // Add default locale info to SOAP header Localization objDefaultLocale = new Localization(); objDefaultLocale.Locale = "en-US"; // Build the Create action for a document CreateAction objCreate = new CreateAction(); objCreate.classId = "Document"; // Build the Checkin action CheckinAction objCheckin = new CheckinAction(); objCheckin.checkinMinorVersion = true; objCheckin.checkinMinorVersionSpecified = true; // Assign the actions to the ChangeRequestType element ChangeRequestType objChangeRequestType = new ChangeRequestType(); objChangeRequestType.Action = new ActionType[2]; objChangeRequestType.Action[0] = (ActionType)objCreate; // Assign Create action objChangeRequestType.Action[1] = (ActionType)objCheckin; // Assign Checkin action // Specify the target object (an object store) for the actions objChangeRequestType.TargetSpecification = new ObjectReference(); objChangeRequestType.TargetSpecification.classId = "ObjectStore"; objChangeRequestType.TargetSpecification.objectId = strObjStoreName; objChangeRequestType.id = "1"; // Build a list of properties to set in the new doc ModifiablePropertyType[] objInputProps = new ModifiablePropertyType[2]; // Specify and set a string-valued property for the DocumentTitle property SingletonString prpDocumentTitle = new SingletonString(); prpDocumentTitle.Value = strDocTitle; prpDocumentTitle.propertyId = "DocumentTitle"; objInputProps[0] = prpDocumentTitle; // 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 prpContentElement = new ListOfObject(); prpContentElement.propertyId = "ContentElements"; prpContentElement.Value = new DependentObjectType[1]; prpContentElement.Value[0] = objContentTransfer; // Read data stream from file containing the document content InlineContent objInlineContent = new InlineContent(); System.IO.Stream inputStream = System.IO.File.OpenRead(strContentFilePath); objInlineContent.Binary = new byte[inputStream.Length]; inputStream.Read(objInlineContent.Binary, 0, (int)inputStream.Length); inputStream.Close(); // Create reference to Content pseudo-property ContentData prpContent = new ContentData(); prpContent.Value = (ContentType)objInlineContent; prpContent.propertyId = "Content"; // Assign Content property to ContentTransfer object objContentTransfer.Property[0] = prpContent; // Create and assign ContentType string-valued property to ContentTransfer object SingletonString prpContentType = new SingletonString(); prpContentType.propertyId = "ContentType"; prpContentType.Value = "application/msword"; // Set MIME-type to MS Word objContentTransfer.Property[1] = prpContentType; objInputProps[1] = prpContentElement; // Assign list of document properties to set in ChangeRequestType element objChangeRequestType.ActionProperties = objInputProps; // Build a list of properties to exclude on the new doc object that will be returned string[] strExcludeProps = new string[2]; strExcludeProps[0] = "Owner"; strExcludeProps[1] = "DateLastModified"; // Assign the list of excluded properties to the ChangeRequestType element objChangeRequestType.RefreshFilter = new PropertyFilterType(); objChangeRequestType.RefreshFilter.ExcludeProperties = strExcludeProps; // Create array of ChangeRequestType elements and assign ChangeRequestType element to it ChangeRequestType[] objChangeRequestTypeArray = new ChangeRequestType[1]; objChangeRequestTypeArray[0] = objChangeRequestType; // Create ChangeResponseType element array ChangeResponseType[] objChangeResponseTypeArray = null; // Build ExecuteChangesRequest element and assign ChangeRequestType element array to it ExecuteChangesRequest objExecuteChangesRequest = new ExecuteChangesRequest(); objExecuteChangesRequest.ChangeRequest = objChangeRequestTypeArray; objExecuteChangesRequest.refresh = true; // return a refreshed object objExecuteChangesRequest.refreshSpecified = true; try { // Call ExecuteChanges operation to implement the doc creation and checkin objChangeResponseTypeArray = wseService.ExecuteChanges(objExecuteChangesRequest); } 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 (objChangeResponseTypeArray==null || objChangeResponseTypeArray.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 prpProperty in objChangeResponseTypeArray[0].Property) { // If property found, store its value if (prpProperty.propertyId == "DocumentTitle") { strDocTitle = ((SingletonString)prpProperty).Value; break; } } // Capture value of the DateCreated property in the returned doc object foreach (PropertyType prpProperty in objChangeResponseTypeArray[0].Property) { // If property found, store its value if (prpProperty.propertyId == "DateCreated") { dateCreated = ((SingletonDateTime)prpProperty).Value; break; } } Console.WriteLine("The document " + strDocTitle + " was successfully created " + dateCreated + "."); Console.WriteLine("Press Enter to end"); Console.ReadLine(); } } }