Dmytro Pastovenskyi
Dmytro Pastovenskyi

Reputation: 5419

Open first attachment from document via URL

I'm looking if it is possible to open attachment from document without specifying the name of attachment because there is only 1 attachment per document.

so f.x. instead of domain/view/documentKey/$FILE/attachmentName I would like to have something similar to this domain/view/documentKey/$FILE/$firstAttachment

Is it possible?

Upvotes: 2

Views: 3574

Answers (5)

andora
andora

Reputation: 1356

Fairly easy to do with an LotusScript agent too. Call an agent with a parameter to identify the location of the attachment, either UNID or document 'key'. The agent name and parameters can be put in a clickable link/anchor tag. When the agent runs, locate the document with the attachment, identify the 'first' attachment and then redirect to open it. This parallels the XPage approach, outlined in other answers.

<a href="file?open&UnidOrKey">Open attachment</a>

Where 'file' is the name of the agent, then in the agent code:

Sub Initialize  
    'open first attachment
    'outline
    'find first attach
    'redirect to open it by name
    
    Dim s As New NotesSession
    Dim db As NotesDatabase
    Dim ctx As NotesDocument
    Dim doc As NotesDocument
    Dim qs As String
    Dim unid As String
    Dim files as variant
    
    Set db = s.Currentdatabase
    Set ctx = s.Documentcontext
    qs = ctx.Query_String_Decoded(0)
    unid = StrRight(qs, "&")
    
    Set doc = vw.Getdocumentby(unid)
    If doc Is Nothing Then
        Print "Not found"
    Else    
        'code to get filelist
        files = Eval(@AttachmentNames, doc)
        'Also possible to get attached files with eval!
        If isempty(Files) Then
            Print "No files found"
        Else
            Print "[0/" & unid & "/$file/" & files(0) & "]"
            'Alternative redirect:
            'Print "location: 0/" & unid & "/$file/" & files(0)
        End If
    End If
End Sub

It's also possible to pass in a document title string and look this up in a view using any key and add additional parameters to select any attachment, either by position (get Nth) or otherwise choose one of the files. Running an agent may be a bit of extra overhead, but it is very flexible; you can add click logging and take other background actions if necessary.

Upvotes: 0

Mike
Mike

Reputation: 1

I would recommend creating your own dummy url parameter like this below:

http://dummy.url.com/database.nsf/0/documentunid?opendocument&dummyurlparameter=yes

Then add code via javascript to your form via computed text like this

"window.location.replace(\"/" + @WebDbName + "/0/" + @Text(@DocumentUniqueID) + 
"/$file/" + @URLEncode("Domino";@Subset(@AttachmentNames;1)) + "\")"

You can then either put this code inside an @If or use a hide formula on this text based on

@URLQueryString("urlparameter") != "yes"

The result will then be that if you pass a normal document open url, it opens the record, and if you include the new parameter, it opens the first attachment.

Alternately, depending on where this url to open the document will be (for example if this is in a view), you could generate the url to open the attachment directly using the same trick as in the JS code above.

Upvotes: 0

Knut Herrmann
Knut Herrmann

Reputation: 30960

This a solution to open document's first attachment using an XPage.

The URL to open attachment is like this

http://Server/Database.nsf/openAttachment.xsp?id=8f29ad7c7e86d3edc1257b65005ab815

Parameter id= has the DocumentUniqueID of document containing attachments. The XPages "openAttachment" has this code

<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core">
    <xp:this.beforePageLoad><![CDATA[#{javascript: 
        var docId = context.getUrlParameter('id');
        var att = session.evaluate("@AttachmentNames", database.getDocumentByUNID(docId));
        if (att[0] != "") {
            var url = context.getUrl().toString().split(view.getPageName())[0] + "/0/" + docId + "/$FILE/" + att[0];
            facesContext.getExternalContext().redirect(url)
        }}]]>
    </xp:this.beforePageLoad>
    Sorry, document has no attachments
</xp:view>

If the document has an attachment the first attachment gets open in browser window or offered for download. If the document has no attachment it shows "Sorry, document has no attachments".

Upvotes: 3

Simon O&#39;Doherty
Simon O&#39;Doherty

Reputation: 9359

First you need to create a rich text field on your form. After that, open the Form properties dialog and click on Launch tab (Rocket).

You will now have extra options to launch the first attachment.

enter image description here

[edit] URL solution if the attachment is an image.

Create a "RichText Lite" field. Set it to "Thumbnail", but do not resize it. In the Image attachment name give it a meaningful name. eg. "image.png" (image shows LAttachment).

enter image description here

After that you can access the attachment directly.

Example:

http://SERVER/DATABASE.nsf/DOC-UNID/$FILE/image.png

Replace the following.

  • SERVER = Domino server.
  • DATABASE = Database the document is in.
  • DOC-UNID = The Documents UNID number

If you open the document in a browser, you will get a more exact URL structure.

Upvotes: 0

Knut Herrmann
Knut Herrmann

Reputation: 30960

I don't think it does exist an URL for that "out of the box". But why don't you extend your own code Rewriting URL in Domino using DSAPI and replace in URL "$firstAttachment" with the first attachment name of document (I know, it takes some effort to find the document)?

Upvotes: 1

Related Questions