DXL Import and Export Not Working in one agent

Hello…

I have created an agent to set the run on behalf property of an agent.

I am doing this using DXL Export Import.

In my agent I do the following:-

  1. Get the handle of the agent As a Notes Document.

  2. Export this document using DXL to a file.

  3. Read the File as a Stream, and get the string contents of the stream.

  4. Add the runonbehalf code into the string.

  5. Write the string to a new stream.

  6. Import using the stream.

  7. Sign the agent.

The problem is, the above works excellent if Export and Import are run as 2 different agents. If I call the export and then import function in the same agent one after the other, I get no error, but the run on behalf value does not get set.

Thanks.

Subject: DXL Import and Export Not Working in one agent

Posting code might help.

Subject: RE: DXL Import and Export Not Working in one agent

This is the code>>>

Sub Initialize

On Error Goto HERE



Set session = New NotesSession



Set db = session.CurrentDatabase

Dim ag As NotesAgent	

Dim docag As NotesDocument



Set ag=db.GetAgent("testrun")	

xurl = ag.NotesURL



'formtitle = doc.~$Title(0)

'formhistory = doc.~$UpdatedBy

'formdesignerversion = doc.~$DesignerVersion(0)



xp = Instr( xurl, ".nsf" )

xunid = Mid( xurl, xp+5, 32)	

Set docag = db.getdocumentbyunid( xunid )



Call Export(docag)





Call Import()





Exit Sub

HERE:

Msgbox Error() & " on  line " & Erl()

Exit Sub

End Sub

Function Export(docag As NotesDocument)

On Error Goto HERE



Dim stream As NotesStream



Set stream = session.CreateStream

filename$ = "c:\ForDevp.xml"

Print filename$ 



If Not stream.Open(filename$) Then

	Messagebox "Cannot open " & filename$,, "Error"

	Exit Function

End If



Call stream.Truncate

REM Export current database as DXL

Dim exporter As NotesDXLExporter

Set exporter = session.CreateDXLExporter

Call exporter.SetInput(docag)	

Call exporter.SetOutput(stream)

Call exporter.Process	

’ Call Stream.Close

Exit Function

HERE:

Msgbox Error() & " on  line " & Erl()

End Function

Function Import()

On Error Goto HERE



'Note  These constants are new with Release 6.

Const DXLIMPORTOPTION_CREATE  = 1

Const DXLIMPORTOPTION_IGNORE = 2

Const DXLIMPORTOPTION_REPLACE_ELSE_IGNORE = 5

Const DXLIMPORTOPTION_REPLACE_ELSE_CREATE  =6

Const DXLIMPORTOPTION_UPDATE_ELSE_IGNORE =9

Const DXLIMPORTOPTION_UPDATE_ELSE_CREATE =10



Dim stream1 As NotesStream

Dim stream2 As NotesStream	

Dim strstream1 As String

Dim strstream2 As String





Set stream1 = session.CreateStream

’ If Not stream.Open("c:\temp" & filename$ & “.xml”) Then

If Not stream1.Open("c:\ForDevp.xml") Then

	Messagebox "Cannot open " & filename$,, "Error"

	Exit Function

End If



If stream1.Bytes = 0 Then

	Messagebox "File did not exist or was empty",, filename$

	Exit Function

End If



Set stream2 = session.CreateStream



'Insert the RUNONBEHALF

strstream1=stream1.ReadText	

firstpart=Strleft(strstream1,"designerversion")

'Msgbox firstpart

newpart="runonbehalfof='"+ session.EffectiveUserName+"'"

lastpart=Strright(strstream1,"designerversion")

'Msgbox lastpart

strstream2=firstpart+" " + newpart +" designerversion" + lastpart

Msgbox strstream1+ Chr(13) + strstream2

'Msgbox strstream1

Call stream2.WriteText(strstream2)

Msgbox stream2.ReadText

REM Import DXL into new database

Dim importer As NotesDXLImporter

Set importer = session.CreateDXLImporter(stream2, db)



importer.ReplicaRequiredForReplaceOrUpdate = False 'Database doesn't need to be a replica

importer.DesignImportOption = DXLIMPORTOPTION_REPLACE_ELSE_CREATE

Call importer.Process

If importer.ImportedNoteCount Then

	Dim strID$

	strID = importer.GetFirstImportedNoteId()

	Do Until strID = ""

		Dim docDesign As NotesDocument

		Set docDesign = db.GetDocumentByID(strID)

		Call docDesign.Sign()

		Call docDesign.Save(True, False, True)

		strID = importer.GetNextImportedNoteId(strID)

	Loop

End If





Exit Function

HERE:

Print Error() & "..." & Erl()

Exit Function

End Function

Thanks.

Subject: RE: DXL Import and Export Not Working in one agent

Have you checked the actual content of the file you’re “creating”? If you don’t kill the file between runs, you will end up adding to the existing file rather than replacing the file content, which will give you an invalid XML document.

Have you ensured that the file write is completed before you attempt to read the file? Again, you will get an invalid XML document if the write is incomplete.

In either case, the error thrown by the NotesDXLImporter should tell you what’s wrong with the DXL document you are trying to import. If you change the Print statement in your error handler (and use a Resume statement in your handlers to avoid a second error) to a Messagebox – or better yet, use a NotesLog object – you’ll have a better chance of getting a readable error message.

For that matter, why create a file at all? You could as easily export to a DOM or SAX parser and inject the runonbehalf there, pipelining the whole operation – which would automatically ensure a valid document (pipelined operations must by definition be completed before the next operation can occur) and, since the whole operation happens in volatile memory can never accidentally include multiple XML documents in the same “file”.