The following code example demonstrates how to copy the contents of a document's content element to a local file using the GetContent
operation. This example assumes a web reference named CEWS has been set to the following URI: http://localhost:<port>/wsi/FNCEWS40MTOM/wsdl.
using System; using System.IO; using Microsoft.Web.Services3; using Microsoft.Web.Services3.Security.Tokens; using Microsoft.Web.Services3.Design; using documentgetcontent_ex.CEWS; namespace documentgetcontent_ex { /// <summary> /// Copies the content data of a document's content element to a local file using the GetContent operation. /// </summary> class Class1 { static void Main(string[] args) { const string strUrl = "http://localhost:9080/wsi/FNCEWS40MTOM/"; // Change port number if necessary const string strObjectStoreName = "ContentEngineExs"; // Pre-existing object store const string strDocPath = "/TestFolder/DemoDoc"; // Path to pre-existing document in object store const string strDocContentPath = "c:\\Test\\ContentSave.doc"; // Path to a file for saving document content // 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"; // Set a reference to the document to retrieve ObjectSpecification objDocumentSpec = new ObjectSpecification(); objDocumentSpec.classId = "Document"; objDocumentSpec.path = strDocPath; objDocumentSpec.objectStore = strObjectStoreName; // Create property filter object and set its attributes PropertyFilterType objPropFilter = new PropertyFilterType(); objPropFilter.maxRecursion = 4; objPropFilter.maxRecursionSpecified = true; // Create filter element array to hold IncludeProperties specifications objPropFilter.IncludeProperties = new FilterElementType[4]; // Create filter element for ContentElements property objPropFilter.IncludeProperties[1] = new FilterElementType(); objPropFilter.IncludeProperties[1].Value = "ContentElements"; // Create an object request for a GetObjects operation ObjectRequestType[] objObjectRequestTypeArray = new ObjectRequestType[1]; objObjectRequestTypeArray[0] = new ObjectRequestType(); objObjectRequestTypeArray[0].SourceSpecification = objDocumentSpec; objObjectRequestTypeArray[0].PropertyFilter = objPropFilter; // Call GetObjects operation to get document object and its properties ObjectResponseType[] objObjectResponseTypeArray; try { objObjectResponseTypeArray = wseService.GetObjects(objObjectRequestTypeArray); } catch(System.Net.WebException ex) { Console.WriteLine("An exception occurred while requesting a document: [" + ex.Message + "]"); return; } // Get document object from the GetObjects response ObjectValue objDocument = null; if (objObjectResponseTypeArray[0] is SingleObjectResponse) { objDocument = ((SingleObjectResponse)objObjectResponseTypeArray[0]).Object; } // Get the ContentElements property of the Document object ListOfObject prpContentElements = null; foreach (PropertyType prpProperty in objDocument.Property) { if (prpProperty.propertyId == "ContentElements") { prpContentElements = (ListOfObject)prpProperty; break; } } // Get number of content elements int intElementCount = (prpContentElements.Value == null) ? 0 : prpContentElements.Value.Length; if (intElementCount == 0) { Console.WriteLine("The selected document has no content elements"); Console.WriteLine("Press Enter to end"); Console.ReadLine(); return; } // Get the content from each content element of the document for (int intElem = 0; intElem < intElementCount; intElem++) { // Get a ContentTransfer object from the ContentElements property collection DependentObjectType objContentTransfer = prpContentElements.Value[intElem]; // Construct element specification for GetContent request ElementSpecificationType objElemSpecType = new ElementSpecificationType(); objElemSpecType.itemIndex = intElem; objElemSpecType.itemIndexSpecified = true; objElemSpecType.elementSequenceNumber = 0; objElemSpecType.elementSequenceNumberSpecified = false; // Construct the GetContent request ContentRequestType objContentReqType = new ContentRequestType(); objContentReqType.cacheAllowed = true; objContentReqType.cacheAllowedSpecified = true; objContentReqType.id = "1"; objContentReqType.maxBytes = 100 * 1024; objContentReqType.maxBytesSpecified = true; objContentReqType.startOffset = 0; objContentReqType.startOffsetSpecified = true; objContentReqType.continueFrom = null; objContentReqType.ElementSpecification = objElemSpecType; objContentReqType.SourceSpecification = objDocumentSpec; ContentRequestType[] objContentReqTypeArray = new ContentRequestType[1];
objContentReqTypeArray[0] = objContentReqType; GetContentRequest objGetContentReq = new GetContentRequest(); objGetContentReq.ContentRequest = objContentReqTypeArray; objGetContentReq.validateOnly = false; objGetContentReq.validateOnlySpecified = true; // Call the GetContent operation ContentResponseType[] objContentRespTypeArray = null; try { objContentRespTypeArray = wseService.GetContent(objGetContentReq); } catch (System.Net.WebException ex) { Console.WriteLine("An exception occurred while fetching content from a content element: [" + ex.Message + "]"); Console.WriteLine("Press Enter to end"); Console.ReadLine(); return; } catch (Exception allEx) { Console.WriteLine("An exception occurred: [" + allEx.Message + "]"); Console.WriteLine("Press Enter to end"); Console.ReadLine(); return; } // Process GetContent response ContentResponseType objContentRespType = objContentRespTypeArray[0]; if (objContentRespType is ContentErrorResponse) { ContentErrorResponse objContentErrorResp = (ContentErrorResponse)objContentRespType; ErrorStackType objErrorStackType = objContentErrorResp.ErrorStack; ErrorRecordType objErrorRecordType = objErrorStackType.ErrorRecord[0]; Console.WriteLine("Error [" + objErrorRecordType.Description + "] occurred. " + " Err source is [" + objErrorRecordType.Source + "]"); Console.WriteLine("Press Enter to end"); Console.ReadLine(); return; } else if (objContentRespType is ContentElementResponse) { ContentElementResponse objContentElemResp = (ContentElementResponse)objContentRespType; InlineContent objInlineContent = (InlineContent)objContentElemResp.Content; // Write content to file Stream outputStream = File.OpenWrite(strDocContentPath); outputStream.Write(objInlineContent.Binary, 0, objInlineContent.Binary.Length); outputStream.Close(); Console.WriteLine("Document content has been written"); Console.WriteLine("Press Enter to end"); Console.ReadLine(); } else { Console.WriteLine("Unknown data type returned in content response: [" + objContentRespType.GetType().ToString() + "]"); Console.WriteLine("Press Enter to end"); Console.ReadLine(); return; } } } } }