Simple document download

Background

In a previous post I showed how to patch the ServiceAPI TrimClient to fix a problem with downloading child documents (e.g. record revisions).  A customer (Mark from WA) pointed out a way to download a file to a stream without having to write it to a file.

Examples

The following code downloads a document using a stream.  I then save to a file but it could also be passed on as a stream or byte array to another system.

Stream stream = trimClient.Get<Stream>(string.Format("Record/{0}/File/Document", 1843));
using (FileStream fs = File.Create("d:\\junk\\myfile.xml"))
{
    stream.CopyTo(fs);
}

Below is the code to use to get a record revision as a stream.

Stream stream = trimClient.Get<Stream>(string.Format("Record/{0}/RecordRevision/{1}", 1843, 12));

Side benefits

Two side benefits of this approach are:

  1. not having to write a file a file unnecessarily, and
  2. avoiding the non-thread safe approach in the previous post.