Highlighted
IdleLoop2
Senior Member.
453 views

.Net SDK V9.1 DocumentStream

It appears that the DocumentStream object has been removed from V9.1.

What is the new method/object for getting a Stream from the .Net SDK for a Document?

0 Likes
14 Replies
JTSR Trusted Contributor.
Trusted Contributor.

Re: .Net SDK V9.1 DocumentStream

I'm facing a similar issue in upgrading from RM8 to CM9. We have a current utility that relies on the DocumentStream method for returning a document to view in the browser (or prompt to download).

What's the recommended method to use now for this type of functionality in the CM9 .NET SDK?

0 Likes
Jamie Smith Super Contributor.
Super Contributor.

Re: .Net SDK V9.1 DocumentStream

You can use the downloadnotifier object, described here: http://hprm.info/2018-5-24-stream-a-document-in-the-net-sdk/

Note that the TRIM 9.0 SDK version is different from the version in 9.1+.

0 Likes
JTSR Trusted Contributor.
Trusted Contributor.

Re: .Net SDK V9.1 DocumentStream

Thanks Jamie, I'll look into it.

We're moving to 9.3 so hopefully no major changes there.
0 Likes
JTSR Trusted Contributor.
Trusted Contributor.

Re: .Net SDK V9.1 DocumentStream

I'm struggling to get this implemented in VB. Visual Studio doesn't like notifier.OnChunkAvailable within the using statement:

Using notifier As New DownloadNotifier(db, count, bytesSoFar)
   notifier.OnChunkAvailable += Function(chunk As Byte(), chunkPos As Long, chunkLen As Long, lastChunk As Boolean)
   'Stream will be here
   End Function

   Dim extractDoc As ExtractDocument = record.GetExtractDocument()
   extractDoc.DoExtract(Nothing, True, False, Nothing)
End Using

 

Any help would be appreciated.

0 Likes
Jamie Smith Super Contributor.
Super Contributor.

Re: .Net SDK V9.1 DocumentStream

When you say Visual Studio doesn't like notifier.OnChunkAvailable within the using statement, I'm assuming it's telling you that the member does not exist.  Check that you are linking against the TRIM 9.1 SDK and not earlier versions.  TRIM 9.0 SDK also has the DownloadNotifier class but it is quite different and does not have the OnChunkAvailable event member.

The code I use in C# with TRIM 9.1+ SDK looks like this...

public static bool TryExtractToStream(this Record record, Stream outStream)
{
   ...

   using (var notifier = new DownloadNotifier(record.Database))
   {
      notifier.OnChunkAvailable += (byte[] chunk, long chunkPos, long chunkLen, bool lastChunk) => {
      outStream.Write(chunk, 0, chunk.Length);
   };

   ...

}

0 Likes
JTSR Trusted Contributor.
Trusted Contributor.

Re: .Net SDK V9.1 DocumentStream

Thanks Jamie.

I'm stuck in Visual Studio dll hell! I have the correct version of HP.HPTRIM.SDK.dll in the bin folder (9.3.2.418), I've removed the reference from the project and added it again, however the properties still show V8.2.0.0 in the reference explorer.

Hopefully once I get VS to play nicely, I'll be able to actually get the downloadnotifier working!

0 Likes
JTSR Trusted Contributor.
Trusted Contributor.

Re: .Net SDK V9.1 DocumentStream

Can anyone that is using v9.3 tell me what their csproj or vbproj files shows for the HP.HPTRIM.SDK reference?

0 Likes
JanMartin Outstanding Contributor.
Outstanding Contributor.

Re: .Net SDK V9.1 DocumentStream

Mine says version 8.2.0.0 no matter which version of CM I have it pointing to - I suspect there is some metadata field that VS is using that hasn't been kept up to date.

0 Likes
JTSR Trusted Contributor.
Trusted Contributor.

Re: .Net SDK V9.1 DocumentStream

Excellent, it means I'm not going nuts! Mine says 8.2.0.0 as well.

I've checked the GAC and the HP.HPTRIM.SDK isn't loaded, the version in the bin folder is correct, and I've removed and re-added the reference to the project.

Back to the drawing board, I'd love to get this working 🙂

0 Likes
JanMartin Outstanding Contributor.
Outstanding Contributor.

Re: .Net SDK V9.1 DocumentStream

OnChunkAvailable is a delegate, I'm not sure how delegates work in VB - perhaps you can't assign them the same way you can in C#?

 

 

0 Likes
JTSR Trusted Contributor.
Trusted Contributor.

Re: .Net SDK V9.1 DocumentStream

I'll need to double check this, thanks for the heads up.

0 Likes
JTSR Trusted Contributor.
Trusted Contributor.

Re: .Net SDK V9.1 DocumentStream

I've managed to get most of it working in VB, just missing the final touches. I've outlined below what I have setup to work with the .NET SDK 9.3.

I have an .aspx page with the function call:

Dim bDocument As Byte() = Nothing
bDocument = TrimUtils.BusinessFunctions.getDocumentForDownload(tmpDatabase, tRecord)
Response.BinaryWrite(bDocument.ToArray())

Within the BusinessFunctions class, the function definition for getDocumentForDownload is:

Public Shared Function getDocumentForDownload(ByVal db As Database, ByVal trimRecord As Record) As Byte()
            Dim count As Long = 20922
            'Dim buffer As Byte() = New Byte(20922) {}
            Dim record As Record = New Record(db, trimRecord.Number)
            Dim bytesSoFar As Long = 0

            Try
                Using notifier As New DownloadNotifier(db, count, bytesSoFar)
                    AddHandler notifier.OnChunkAvailable, AddressOf BusinessFunctions.Notifier_OnChunkAvailable

                    'Missing return? Should the buffer be returned? How?

                    Dim extractDoc As ExtractDocument = record.GetExtractDocument()
                    extractDoc.DoExtract(Nothing, True, False, Nothing)
                End Using
            Catch ex As Exception
                'Error handling
            End Try
        End Function
Public Shared Sub Notifier_OnChunkAvailable(ByVal chunk As Byte(), ByVal chunkPos As Long, ByVal chunkLen As Long, ByVal lastChunk As Boolean)
            Dim byteArray(256) As Byte
            Dim ms As MemoryStream

            Try
                ms = New MemoryStream(256)
                ms.Write(chunk, 128, 256)
                byteArray = ms.ToArray()
                ms.Close()
            Catch ex As Exception
                'Error handling
            End Try
        End Sub

 

The error I'm receiving looks related to the fact that bDocument is null.

Exception Message: Value cannot be null. Parameter name: source Stack: at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)

How do I return the memory stream data to the .aspx page that is calling the function? 

Any help would be appreciated.

0 Likes
Jamie Smith Super Contributor.
Super Contributor.

Re: .Net SDK V9.1 DocumentStream

Assuming you are using ASP.net / System.Web, the easiest way is to copy the memory stream to the output stream of the HttpResponse object.  You should set the content type and disposition to indicate file/atttachment. Something like ...

var memStream = .....

HttpContext context = ....

memStream.CopyTo(context.Response.OutputStream);

0 Likes
JTSR Trusted Contributor.
Trusted Contributor.

Re: .Net SDK V9.1 DocumentStream

Thanks Jamie, much appreciated.

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.