Create new FileNet document instance using image content of exist document instance Using FileNet Web Service
Introduction
I have wrote this post to tell you how to Create new FileNet document instance using image content of exist document instance Using FileNet Web Service. Really I have spent some hours to implement this solution and I want to share it with you to save your time.
Assumption
I will assume the following points so please, do not forget to follow these points for future use:
1- The Web Service proxy named FNWSP8351.
2- The main namespace named FileNetWS
3- I will download the image from source document at the following path c:\Temp\Images\
4- I have built new class to contain all FileNet configuration that named FNWSConfiguration and the below list of codes contain implementation of that class .
1: Public Class FNWSConfiguration
2: Private Shared mWSUrl As String = CSCSettingReader.FNWSURL
3: Private Shared mUserName As String = CSCSettingReader.FNWSUserName
4: Private Shared mPassword As String = CSCSettingReader.FNWSPassword
5: Private Shared mObjectStore As String = CSCSettingReader.FNWSObjectStoreName
6: Public Shared ReadOnly Property WSUrl() As String
7: Get
8: Return mWSUrl
9: End Get
10: End Property
11: Public Shared ReadOnly Property UserName() As String
12: Get
13: Return mUserName
14: End Get
15: End Property
16: Public Shared ReadOnly Property Password() As String
17: Get
18: Return mPassword
19: End Get
20: End Property
21: Public Shared ReadOnly Property ObjectStore() As String
22: Get
23: Return mObjectStore
24: End Get
25: End Property
26: End Class
Implementation
The below list of code has complete implementation for Create new FileNet document instance using image content of exist document instance, I will explain the code after this code snippet.
1: Imports System.Configuration.ConfigurationManager
2: Imports System.IO
3: Public Class FNWSHelper
4: Public Property WSobjBinding() As FNWSP8351.FNCEWS35ServiceWse
5: Get
6: Return mWSobjBinding
7: End Get
8: Set(ByVal value As FNWSP8351.FNCEWS35ServiceWse)
9: mWSobjBinding = value
10: End Set
11: End Property
12: Sub New()
13: mWSobjBinding = LoginFNWS()
14: End Sub
15:
16: Private Function LoginFNWS() As FNWSP8351.FNCEWS35ServiceWse
17: Try
18: ' Get the WS binding object and extract the WSE SoapContext for it
19: ' (this will be used to add an attachment, and to set WS-Security parameters)
20: Dim objBinding As FNWSP8351.FNCEWS35ServiceWse = New FNWSP8351.FNCEWS35ServiceWse
21: Dim objCtx As Microsoft.Web.Services2.SoapContext
22: objCtx = objBinding.RequestSoapContext ' Create a ChangeRequest and populate it
23:
24:
25: ' Fill in the security headers...
26: Dim strUser As String = FNWSConfiguration.UserName
27: Dim tok As Microsoft.Web.Services2.Security.Tokens.UsernameToken
28:
29: tok = New Microsoft.Web.Services2.Security.Tokens.UsernameToken(strUser, FNWSConfiguration.Password, _
30: Microsoft.Web.Services2.Security.Tokens.PasswordOption.SendPlainText)
31: objCtx.Security.Tokens.Add(tok)
32: objBinding.Url = FNWSConfiguration.WSUrl
33: Return objBinding
34: Catch ex As Exception
35: End Try
36:
37: End Function
38: Public Function AddDocument(ByVal ID As String) As Boolean
39: Try
40: Dim docpropcount As Integer = 2
41: ' Create a ChangeRequest and populate it
42: Dim objChangeRequest As FNWSP8351.ChangeRequestType = New FNWSP8351.ChangeRequestType
43: objChangeRequest.Action = New FNWSP8351.ActionType(2) {}
44:
45: PrepareDocumentObj(objChangeRequest, dr("DocClassName"))
46:
47: ' Build a list of properties to set in the new document
48: objChangeRequest.ActionProperties = PrepareDocumentProperties(docpropcount, dr)
49: 'attach doc
50: If Not IsImageExist(ID + ".tif") Then
51: GetDocumentContentByID(ID)
52: End If
53: AttachDocumentContent(objChangeRequest.ActionProperties, objChangeRequest, ID + ".tif")
54:
55: PrepareExcludedProperties(objChangeRequest)
56:
57:
58: Dim objResponseArray() As FNWSP8351.ChangeResponseType
59: objResponseArray = New FNWSP8351.ChangeResponseType() {}
60:
61: objResponseArray = WSobjBinding.ExecuteChanges(FinalizeRequest(objChangeRequest))
62: Return True
63: Catch ex As Exception
64:
65: Return False
66: End Try
67:
68: End Function
69:
70: Private Sub PrepareDocumentObj(ByVal objChangeRequest As FNWSP8351.ChangeRequestType, ByVal docclassname As String)
71: Try
72: Dim CreateVerb As FNWSP8351.CreateAction = New FNWSP8351.CreateAction
73: CreateVerb.classId = docclassname
74:
75: Dim chkin As New FNWSP8351.CheckinAction
76: chkin.checkinMinorVersion = False
77: chkin.checkinMinorVersionSpecified = False
78: objChangeRequest.Action(0) = CreateVerb
79: objChangeRequest.Action(1) = CType(chkin, FNWSP8351.ActionType)
80: CType(chkin, FNWSP8351.CheckinAction).checkinMinorVersion = False
81: CType(chkin, FNWSP8351.CheckinAction).checkinMinorVersionSpecified = False
82: objChangeRequest.TargetSpecification = New FNWSP8351.ObjectReference
83: objChangeRequest.TargetSpecification.classId = "ObjectStore"
84: objChangeRequest.TargetSpecification.objectId = FNWSConfiguration.ObjectStore
85: objChangeRequest.id = "1"
86: Catch ex As Exception
87:
88: End Try
89:
90: End Sub
91:
92: Private Function PrepareDocumentProperties(ByVal docpropcount As Integer, ByVal dr As DataRow) As FNWSP8351.ModifiablePropertyType()
93: Try
94: Dim objInputProps As FNWSP8351.ModifiablePropertyType()
95: objInputProps = New FNWSP8351.ModifiablePropertyType(docpropcount) {}
96:
97: objInputProps(0) = GetDocPropertyObject(subjectcodesymbolicname, "Subject")
98: Return objInputProps
99: Catch ex As Exception
100:
101: End Try
102:
103: End Function
104:
105:
106: Private Function GetDocPropertyObject(ByVal symbolicname As String, ByVal propval As Object)
107: Dim Propobj
108: Select Case symbolicname
109: Case "Subject"
110: Propobj = New FNWSP8351.SingletonString
111: End Select
112: Propobj.Value = propval
113: Propobj.propertyId = symbolicname
114: Return Propobj
115: End Function
116:
117: Private Function GetDocumentContentByID(ByVal docid As String)
118: 'prepare document object
119: Dim objSpec As FNWSP8351.ObjectSpecification = New FNWSP8351.ObjectSpecification
120: objSpec.objectId = docid
121: Dim objRequest As FNWSP8351.ObjectRequestType = New FNWSP8351.ObjectRequestType
122: objSpec.classId = "Document"
123: objSpec.objectStore = FNWSConfiguration.ObjectStore
124: objRequest.SourceSpecification = objSpec
125: objRequest.id = "1"
126:
127: 'prepare request properties
128: Dim incProps() As FNWSP8351.FilterElementType
129: objRequest.PropertyFilter = New FNWSP8351.PropertyFilterType
130:
131:
132: ' Ask for the content properties...
133: Dim maxSize As UInt64 = Convert.ToUInt64(1000000)
134:
135: incProps = New FNWSP8351.FilterElementType(4) {}
136: incProps(0) = New FNWSP8351.FilterElementType
137: incProps(0).Value = "ContentElements"
138: incProps(1) = New FNWSP8351.FilterElementType
139: incProps(1).Value = "ContentData"
140: incProps(1).maxSize = maxSize
141: incProps(1).maxSizeSpecified = True
142: incProps(2) = New FNWSP8351.FilterElementType
143: incProps(2).Value = "Content"
144: incProps(3) = New FNWSP8351.FilterElementType
145: incProps(3).Value = "DocumentTitle"
146: incProps(4) = New FNWSP8351.FilterElementType
147: incProps(4).Value = "ContentType"
148:
149: objRequest.PropertyFilter.IncludeProperties = incProps
150: objRequest.PropertyFilter.maxRecursion = 1
151: objRequest.PropertyFilter.maxRecursionSpecified = True
152:
153: ' Create the request array
154: Dim objRequestArray() As FNWSP8351.ObjectRequestType = New FNWSP8351.ObjectRequestType(1) {}
155: objRequestArray(0) = objRequest
156:
157: ' Fill in the security headers...
158: Dim objDimeBinding As FNWSP8351.FNCEWS35ServiceWse = New FNWSP8351.FNCEWS35ServiceWse
159: Dim objCtx As Microsoft.Web.Services2.SoapContext = objDimeBinding.RequestSoapContext
160: Dim strUser As String = FNWSConfiguration.UserName
161: Dim tok As Microsoft.Web.Services2.Security.Tokens.UsernameToken
162: tok = New Microsoft.Web.Services2.Security.Tokens.UsernameToken(strUser, _
163: FNWSConfiguration.Password, _
164: Microsoft.Web.Services2.Security.Tokens.PasswordOption.SendPlainText)
165: objCtx.Security.Tokens.Add(tok)
166: objDimeBinding.Url = FNWSConfiguration.WSUrl
167:
168: ' Send off the request
169: Dim objResponseArray() As FNWSP8351.ObjectResponseType
170: objResponseArray = New FNWSP8351.ObjectResponseType() {}
171: Try
172: objResponseArray = objDimeBinding.GetObjects(objRequestArray)
173: Catch Ex As Exception
174: Exit Function
175: End Try
176:
177:
178: If objResponseArray(0).GetType().FullName.Contains("ErrorStackResponse") Then
179: Dim objErrResp As FNWSP8351.ErrorStackResponse = objResponseArray(0)
180: Dim objStack As FNWSP8351.ErrorStackType = objErrResp.ErrorStack
181: Dim objErr As FNWSP8351.ErrorRecordType = objStack.ErrorRecord(0)
182:
183: Exit Function
184: End If
185:
186: ' Extract the document object from the response
187: Dim objDoc As FNWSP8351.ObjectValue
188: If objResponseArray(0).GetType().FullName.Contains("SingleObjectResponse") Then
189: Dim objSingleObjResponse As FNWSP8351.SingleObjectResponse = objResponseArray(0)
190: objDoc = objSingleObjResponse.Object
191: ElseIf objResponseArray(0).GetType().FullName.Contains("ObjectSetResponse") Then
192: Dim objSetResponse As FNWSP8351.ObjectSetResponse = objResponseArray(0)
193: Dim objSet As FNWSP8351.ObjectSetType = objSetResponse.ObjectSet
194: objDoc = objSet.Object(0)
195: Else
196: Exit Function
197: End If
198:
199: Dim objResponseContext As Microsoft.Web.Services2.SoapContext = objDimeBinding.ResponseSoapContext
200: If (Not (objResponseContext Is Nothing)) And (Not (objResponseContext.Attachments Is Nothing)) Then
201: Dim att As Microsoft.Web.Services2.Attachments.Attachment
202: Dim len As Integer
203: Dim nItem As Integer = 0
204: Dim byteContent() As Byte
205: For Each att In objResponseContext.Attachments
206: Dim objStream As System.IO.Stream = att.Stream
207: byteContent = New Byte(objStream.Length) {}
208: len = objStream.Read(byteContent, 0, objStream.Length)
209:
210: ' Write it out to a file
211: Dim now As System.DateTime = System.DateTime.Now
212: Dim strFileName As String = "C:\Temp\Images\" + docid + ".tif"
213: saveContentToFile(strFileName, byteContent)
214: nItem = nItem + 1
215: Next
216: End If
217:
218: End Function
219:
220: Private Sub saveContentToFile(ByVal strFileName As String, ByVal binaryData() As Byte)
221: Try
222: Dim outFile As System.IO.FileStream = New System.IO.FileStream(strFileName, _
223: System.IO.FileMode.CreateNew, _
224: System.IO.FileAccess.Write)
225: outFile.Write(binaryData, 0, binaryData.Length)
226: outFile.Close()
227: Catch Exp As Exception
228: MessageBox.Show("Saving content failed: [" + Exp.Message + "]")
229: End Try
230: End Sub
231:
232:
233: Private Function GetFileName(ByVal filepath As String)
234: Dim indx As Integer = filepath.LastIndexOf("\")
235: Return filepath.Substring(indx + 1)
236: End Function
237: Private Sub AttachDocumentContent(ByVal objInputProps As FNWSP8351.ModifiablePropertyType(), ByRef objChangeRequest As FNWSP8351.ChangeRequestType, ByVal imgpath As String)
238: Try
239: Dim ulContentSize As Integer
240: Dim inFile As System.IO.FileStream
241: Dim strContentLocation As String = "c:\Temp\Images\" + imgpath
242: ' Create the content element list and set it into the document's properties
243: Dim contentObjects() As FNWSP8351.DependentObjectType
244: contentObjects = New FNWSP8351.DependentObjectType(1) {}
245: Dim objContentList As FNWSP8351.ListOfObject = New FNWSP8351.ListOfObject
246: objContentList.propertyId = "ContentElements"
247: objContentList.Value = contentObjects
248: objInputProps(5) = objContentList
249:
250: Dim ctProps() As FNWSP8351.PropertyType
251: ctProps = New FNWSP8351.PropertyType(3) {}
252:
253: ' Set the ContentType property
254: Dim typeProp As FNWSP8351.SingletonString = New FNWSP8351.SingletonString
255: typeProp.propertyId = "ContentType"
256: typeProp.Value = GetAttachmentFileType(strContentLocation)
257: ctProps(0) = typeProp
258:
259: ' Create the dependent object type object
260: Dim ct As FNWSP8351.DependentObjectType = New FNWSP8351.DependentObjectType
261: ct.dependentAction = FNWSP8351.DependentObjectTypeDependentAction.Insert
262: ct.dependentActionSpecified = True
263:
264:
265: Dim nameProp As FNWSP8351.SingletonString = New FNWSP8351.SingletonString
266: nameProp.propertyId = "RetrievalName"
267: nameProp.Value = imgpath
268: ctProps(1) = nameProp
269: ' create content type object
270: Dim contType As FNWSP8351.DIMEContent = New FNWSP8351.DIMEContent
271: contType.size.Parse(ulContentSize.ToString())
272:
273: ' Add an attachment (uses the DIME binding)
274: Try
275: Dim att As Microsoft.Web.Services2.Dime.DimeAttachment
276: inFile = New System.IO.FileStream(strContentLocation, _
277: System.IO.FileMode.Open, _
278: System.IO.FileAccess.Read)
279:
280: att = New Microsoft.Web.Services2.Dime.DimeAttachment(GetAttachmentFileType(strContentLocation), _
281: Microsoft.Web.Services2.Dime.TypeFormat.MediaType, _
282: inFile)
283: att.Id = "attachment1"
284: contType.Attachment = New FNWSP8351.DIMEAttachmentReference
285: contType.Attachment.location = "attachment1"
286: Dim objCtx As Microsoft.Web.Services2.SoapContext
287: objCtx = WSobjBinding.RequestSoapContext
288: objCtx.Attachments.Add(att)
289: Catch Ex As Exception
290: Throw New System.Exception(Ex.Message, Ex)
291: End Try
292:
293:
294: ' create content data object
295: Dim contData As FNWSP8351.ContentData = New FNWSP8351.ContentData
296: contData.propertyId = "Content"
297: contData.Value = contType
298: ctProps(2) = contData
299:
300: ' Dependent object is of type ContentTransfer
301: ct.classId = "ContentTransfer"
302: ct.Property = ctProps
303: contentObjects(0) = ct
304:
305: objChangeRequest.ActionProperties = objInputProps
306:
307: Catch ex As Exception
308:
309: End Try
310:
311:
312: End Sub
313:
314: Private Function GetAttachmentFileType(ByVal filepath As String)
315: If filepath.ToLower.Contains(".jpeg") Or filepath.ToLower.Contains(".jpeg") Then
316: Return "image/jpeg"
317: ElseIf filepath.ToLower.Contains(".tif") Or filepath.ToLower.Contains(".tiff") Then
318: Return "image/tiff"
319: ElseIf filepath.ToLower.Contains(".txt") Then
320: Return "text/txt"
321: End If
322: Return "image/jpeg"
323: End Function
324:
325:
326: Private Sub PrepareExcludedProperties(ByRef objChangeRequest As FNWSP8351.ChangeRequestType)
327: ' Build a list of properties to exclude on the refreshed doc object that is returned
328: Dim strExclude() As String = New String(2) {}
329: strExclude(0) = "DateCreated"
330: strExclude(1) = "DateLastModified"
331: objChangeRequest.RefreshFilter = New FNWSP8351.PropertyFilterType
332: objChangeRequest.RefreshFilter.ExcludeProperties = strExclude
333: End Sub
334: Private Function IsImageExist(ByVal docimage As String) As Boolean
335: Dim dir As New DirectoryInfo("C:\Temp\Images")
336: If dir.GetFiles.Length > 0 Then
337: For Each mfile As FileInfo In dir.GetFiles
338: If mfile.Name = docimage Then
339: Return True
340: End If
341: Next
342: End If
343: Return False
344:
345: End Function
346: Private Function FinalizeRequest(ByVal objChangeRequest As FNWSP8351.ChangeRequestType) As FNWSP8351.ExecuteChangesRequest
347: Dim objRequest As FNWSP8351.ExecuteChangesRequest = New FNWSP8351.ExecuteChangesRequest
348: objRequest.ChangeRequest = New FNWSP8351.ChangeRequestType(1) {}
349: objRequest.ChangeRequest(0) = objChangeRequest
350: objRequest.refresh = True
351: objRequest.refreshSpecified = True
352: Return objRequest
353:
354: End Function
355: End Class
Now let’s go deeply in the FNWShelper class, note I will give you high level of understanding and I am not going to explain this class line by line.
- Lines 4 to 11 we create property of the web service extension for reusing it in other places.
- Lines 12 to 14 we create the class constructor and calling the LoginFNWS function .
- Lines 16 to 37 we create the web service extension UserNameToken for preparing login to the FileNet web service.
- Lines 38 to 68 we commit the new document to filenet with image found in the document that has ID passed to that method, now let’s see this AddDocument function in more details.
- Lines 40 to 43 is used for creating a ChangeRequest instance and set the Action Property with new list of two ActionType items.
- Line 45 is calling preparedocumentobj which implemented between lines 70 an 90 at this method we create two ActionType instance one of CreateAction and another one of CheckInAction for check in the committed document at FileNet, while lines 78 to 85 are preparing the ChangeRequestType instance.
- Go back to the adddocument method and see line 48 that used for building the list of properties to be saved in the new document, this line of code calls method named preparedocumentpreopties that implemented between lines 92 and 103 and it just creates instance of ModifiablePreopertyType list of specific length and assign only one property called subject.
- Again back to adddocument method and see line 50,51 and 52 that check if the images folder has image named “dociid.tif” where document ID is the value of document ID passed to this method, If the image found the If statement will be escaped else it will call getdocumentcontentbyid method that will be explained in more details now.
- Lines 117 to 218 we implement method getdocumentbyid that used for saving document image content that has ID passed throw this method to images folder that defined in assumption section.
- Again back to adddocument method and see line no 53 that used to call method named AttachDocumentContent that take the ChangeRequestType instance and the new image path as parameter to attach the passed image to the new document this method is implemented at lines 273 to 312.
- After calling the attachdocumentcontent method in the adddocument method build the excluded list properties for the new document with the same way we define the ModifiablePropertyType list.
- Finally adddocument method submit the request to FileNet at lines 58 to 61 by using filenet web service method called ExecuteChanges.
I understand that implementation is very big and all lines of code note explained in details but I was trying to save your time for searching and implementing this solution and also I was trying to give you quick help and summary about this implementation.
For testing and using this code write the following two line of code
1: dim mFNWSHelper as new FNWSHelper
2: mFNWSHelper.AddDocument("docid")'where docid is the id of the document that you want to copy it image content to the new created document
I hope that was helpful………………….
-
Archives
- May 2011 (2)
- January 2011 (2)
- December 2010 (1)
- September 2010 (8)
- August 2010 (1)
- March 2010 (1)
- November 2009 (1)
- October 2009 (3)
- September 2009 (2)
- August 2009 (1)
- July 2009 (7)
- June 2009 (10)
-
Categories
-
RSS
Entries RSS
Comments RSS

