Search code examples
hp-trim

Getting a URL of a record from HP TRIM's Desktop Client


Is it possible to copy the URL of a Record/Document from HP's TRIM and sent it to someone in order to download?


Solution

  • There are a few ways of going about doing something like this. Assuming you're sending the link to someone on the same WAN, or the security-risky option of having your TRIM system internet accessible, what I'd do is build a simple web service over the top of the TRIM SDK. The TRIM SDK is COM (with a PIA wrapper) or a proper .Net assembly (in version 7.*), so a simple ASP.Net service would be quite easy.

    Below is the code for an ASP.Net service I built, based on a code sample provided by HP (based on the TRIMSDKPIA20.dll, not the TRIM 7.0 HP.HPTRIM.SDK assembly). You could use it as a basis to make something more RESTful, but as it is, you'd call it using a URL like: http://server/ViewRecord/recno=D11-001

    You could then create an "External Link", an Addin based again on the SDK that you can register as a Right-Click option in the TRIM interface. Something like "Send Download URL..." that fires up an email and generates the URL, but that's a bit more complicated.

    Anyway, the code for the webservice:

    using System;
    using System.Collections.Generic;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using TRIMSDK;
    using System.Configuration;
    using Microsoft.Win32;
    using System.IO;
    
    public partial class _Default : System.Web.UI.Page 
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string errormsg = string.Empty;
    
            //Response.Clear();
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Database trim = new Database();
            trim.SetAsWebService();
            trim.Id = ConfigurationSettings.AppSettings["dbid"];
            trim.WorkgroupServerName = ConfigurationSettings.AppSettings["wgserver"];
            trim.WorkgroupServerPort = Int32.Parse(ConfigurationSettings.AppSettings["wgport"]);
            trim.Connect();
            string recno = Request.QueryString["recno"];
            if (String.IsNullOrEmpty(recno))
            {
                errormsg = "No recno parameter was passed.";
            }
            else
            {
                Record rec = trim.GetRecord(recno);
                if (rec == null)
                {
                    errormsg = string.Format("Could not find a record with number \"{0}\". Either it doesn't exist, or you don't have permission to view it.", recno);
                }
                else
                {
                    if (!rec.IsElectronic)
                    {
                        errormsg = string.Format("Record {0} does not have an electronic attachment", rec.Number);
                    }
                    else
                    {
                        IStream s = rec.GetDocumentStream(null, false, null);
    
                        Response.ClearHeaders();
                        Response.AddHeader("Content-Disposition", "filename=" + rec.SuggestedFileName);
                        Response.AddHeader("Content-Length", rec.DocumentSize.ToString());
                        Response.Buffer = false;
                        Response.ContentType = GetContentType(rec.Extension);
    
                        uint BufferSize = 10000;
                        uint DocumentLength = (uint)rec.DocumentSize;
                        byte[] buffer = new byte[BufferSize];
                        uint bytesread;
                        uint totalread = 0;
    
                        Stream outstream = Response.OutputStream;
    
                        while (totalread < DocumentLength)
                        {
                            s.RemoteRead(out buffer[0], 10000, out bytesread);
                            if (bytesread < 10000)
                            {
                                for (uint i = 0; i < bytesread; i++)
                                {
                                    outstream.WriteByte(buffer[i]);
                                }
                            }
                            else
                            {
                                outstream.Write(buffer, 0, 10000);
                            }
                            totalread += bytesread;
                        }
                        outstream.Close();
                        Response.Flush();
                        return;
                    }
                }
            }
            Response.Write(errormsg);
        }
    
        private string GetContentType(string fileExtension)
        {
            string ct = Registry.GetValue(@"HKEY_CLASSES_ROOT\." + fileExtension.ToLower(), "Content Type", string.Empty) as string;
    
            if (ct.Length == 0)
            {
                ct = "application/octet-stream";
            }
    
            return ct;
        }
    }