Tip: This is how you copy attachments without saving to disk

Hi All

A lot of people have posted messages in this forum about copying attachments from one doc to another.

The established method seems to be extracting the attached file(s) from the source doc (doc1) to a disk folder and then attaching the disk files to the target doc (doc2). Then you have to remove each attachment from disk to keep things clean.

This is a waste of resources and quite understandably most people don’t like it.

The code below will copy all the attachments from doc 1 to doc2 all in memory and without extracting to disk etc. This is intended form browser based documents.

Another major advantage of this method is that you don’t need to know the name of a richtextfield holding the attachment.

For example, if a document is accessed via a browser and attachments are created using a file upload control, then you can’t get hold of a richtextfield holding the attachments. And yet most examples of grabbing the attachments use something like:

Set rtitem = doc.GetFirstItem( “Body” )

which means you have to name/select the field that willbe checked for attachments. (By the way I tried doc.EmbeddedObjects but kept getting type mismatch)

With the method below you don’t need to know any field names. I have tested this method with documents in which the attachments are attached using a file upload control on the web and it works very well and works for multiple attachments too:

Call doc1.CopyAllItems(doc2)	

Forall o In doc2.Items

	If o.name <> "$FILE" Then

		doc2.RemoveItem(o.name)

	End If

End Forall	

As you can see all items in doc1 are copied to doc2. Then in doc2 all the items that are not an attachment (“$FILE”) are removed. NOTE: if you check out the name of the item using something like:

Call agentLog.LogAction("item name is: " & o.name)

you will get “$file” in small letters which is very strange because only this capital letters work:

If o.name <> “$FILE” Then

Finally if your target document (doc2) already has items on it which you want to keep, simply create a new temporary document (doc3) that you don’t need to save. Use doc3 in instead of doc2 in the above example. You will then have doc3 which has all the attachments only. You can then do:

Call doc3.CopyAllItems(doc2, false)

which transfers only the attachments to doc2 (your target document).

You can also change things around and remove from (the in-memory) doc1 all items that are not attachments using the above Forall loop. Then copyallitems to doc2. BUT “don’t” save doc1 and you will still have all its original items, when your code stops.

I hope this helps.Good luck

Bizhan

http://www.Internet-Marketing-Wizards.com

Subject: One problem: unable to append attachments

The copyAllItems works fine for me and will copy attachments. My problem happens if I want to preserve existing attachments in the doc to be copied to.

The solution described above does not work for me–ie,

existingDocTobeAppended.CopyAllItems(tempdoc)

Forall o In tempdoc.Items

If o.name <> “$FILE” Then

doc2.RemoveItem(o.name)

End If

DocWithNewAttachments.CopyAllItems(existingDocToBeAppended, true)

tempdoc.CopyAllItems(existingDocToBeAppended,false)

The last step, which I had hoped would append the old attachments back to the doc, does not work. To all appearances, it does nothing. The “existingDocToBeAppended” iis an exact copy of the “newDocWithAttachments”, and old attachments are lost. (domino 6.5.5)

Am I missing something?

Subject: Tip: This is how you copy attachments without saving to disk

crumbs, i can’t keep track of the arguing in this post so i just wanted to thank the person who posted this because you have saved me from throwing my laptop out of the window…

i got 250 results from my search and luckily i clicked on yours - it doesn’t matter if someone else has already posted this, i might not have found theirs…

i had to rewrite the code using java but the solution is very similar… (copyItemToDocument method in java very usefully ignores richtext - AGGGGHHHHHH)

Subject: Sorry

Subject: RE: Any other silly code to share ? Please teach the F1 key instead of this crap.

Yannick

Thank you for your contribution. I have two comments for you:

1- Please do a search for say “copy attachments”.

Currently, you get something like 47 results and not one of them helps you copy an attchment without extracting to disk the way the attachment is uploaded in my particular case as explained in my original post.

Also they all assume that you know the name of a richtextfield to grab the attachments from.

If you use the file upload control and attach a file using your browser you will not get a richtextfield on your Notes document.

You may be smart enough to figure it out for yourself and that’s fine. But think for a moment about the fact that “nobody” has offered a solution in the forum that I could find for my problem. Also note that by now thousdands of people have searched for this issue and read the existing posts and may have gone away thinking there isn’t an ‘in-memory’ solution.

Somebody somewhere will benefit from this however trivial it may be for you. And just becasue the code is tiny doesn’t make it any less valuable to those who have the problem and can’t find a solution in the other posts in the forum.

2- Please take a good look at language of the posts in this forum.

This is not the kind of Intenet forum where people shout at or bad mouth each other. Valid contributions are gratefully accepted by others and those that may not be up to your stanards are simply ignored.

I’d be happy to read your contribution to other people’s learning and progress any time. And I’m not being sarcastic either. I really mean it.

Good luck

Bizhan

http://www.Internet-Marketing-Wizards.com

Subject: I finally read your post

Hi

I’m sorry, I am sick and a lil bit rude, BUT

copying items has been lots of times talked about on this forum,

and the trick

forall it in doc.items

instead of getFirstItem

is known since ages…

All these informations are available in the Designer Help,

A note can have several $File items associated with it. The picture icon of a file attachment that displays in the rich text of a note is merely a reference to the attachment, which is actually stored in the $File item.

“$file”, first result

Forall item In doc.Items
If ( item.IsAuthors ) Then
Set authorItem = item
End If
End Forall

“doc.items”, first result, sample code

I really do not see what is the interest within you post…

“Give a fish to someone, he will eat a day, teach him fishing, he’ll eat all his life”

Excuse me, your code is really good, but not so appropriate as you seem to believe.

ian

Subject: RE: Any other silly code to share ? Please teach the F1 key instead of this crap.

Ah Grasshopper, search and you shall find. :slight_smile:

I did a quick search for “copy and attachments”, and got back 250 results.

Hits 4 and 5 in the results were for a thread called ‘Best way to copy attachments using LS’ which pretty much contains your code verbatim.

http://www-10.lotus.com/ldd/nd6forum.nsf/55c38d716d632d9b8525689b005ba1c0/d7a34818ed6ec90485257154004dd244?OpenDocument

Your code:

Call doc1.CopyAllItems(doc2)

Forall o In doc2.Items

If o.name <> “$FILE” Then

doc2.RemoveItem(o.name)

End If

End Forall

Other code:

Call olddoc.CopyAllItems (tempdoc)

Forall item In docTemp.Items

If Not (Ucase(item.Name) = “$FILE”) Then

docTemp.RemoveItem (item.Name)

End If

End Forall

Call tempdoc.CopyAllItems (newdoc, False)

The moral of the story is that Notes.net is the fount of all wisdom. A decent search and a bit of patience will nearly always find the answer.

Frunobulax

Subject: RE: Any other silly code to share ? Please teach the F1 key instead of this crap.

Nonetheless, Bizhan is absolutely correct that this is not the sort of forum for complaining about other people and shooting them down. I would rather have a dozen posters posting what they thought were helpful tips and solutions and answers than one overly sarcastic person shutting them down, because the tone of the community is important, and can be ruined quite easily. We welcome new voices, and they will eventually, and gently, be set straight if they veer too much from the way the community operates.

Subject: RE: Any other silly code to share ? Please teach the F1 key instead of this crap.

I wasn’t commenting on the tone of the other posters message.

I was merely pointing out that the code is already there if one searches for it.

Frunobulax

Subject: *Understood, and I should probably have posted as a more direct response higher up. Sorry.

Subject: RE: Any other silly code to share ? Please teach the F1 key instead of this crap.

Hi Ben

I am sorry,

I think these are post-work traumas… but there are too many posts meaning nothing I can handle.

You are absolutlely right, but, I really thought this forum was intended for people knowing just how to read,

and/or to press the F1 key. And I’m serious.

Do you know how many people work this silly way inside IBM ???

This is why I resigned a few years ago… but my frisge was empty, so…

By the way, have you read the post about the view exploring ? I do not know what to think about it.

ian

Subject: RE: Any other silly code to share ? Please teach the F1 key instead of this crap.

Yannick,

You have been active in this forum for less than a year. During this time I have seen no constructive or helpful posts from you. You have NOT earned the right to be exasperated with others.

Chill out - stop piling on and flaming people.

Subject: *I disagree. People do ask this question, and I think it is good for people to post tips.

Subject: The best Notes tip in the world - ever!

Everyone should learn how to use the search facility on Notes.net. Period.

The single biggest problem people have with searching Notes.net is that they don’t seperate search terms with conditions.

For example:

BAD SEARCH: “copy rich text” - returns 25 hits.

GOOD SEARCH “copy and rich and text” - returns 250 hits.

Even better, rtfm about how to construct a good search string for Domino:

http://www-12.lotus.com/ldd/doc/domino_notes/7.0/help7_client.nsf/b3266a3c17f9bb7085256b870069c0a9/3b82be24a95493a08525704a004da092?OpenDocument

Frunobulax

Subject: RE: The best Notes tip in the world - ever!

Hi Mr Frunobulax

The problem that my code addresses is where you do ‘not’ have a richtextfield or a field name to play with.

Most of the posts assume you have richtextfield into which the attachment has been added or that you know the name of the richtextfield holding the attachment. That is not the case with my problem.

But I appreciate your post.

Thanks

Bizhan

http://www.Internet-Marketing-Wizards.com

Subject: RE: The best Notes tip in the world - ever!

Or once again., the CRISPY initiative

How to ask a CRISPY question about Notes/Domino —

The CRISPY initiative - a quest for better questions

JYR

Subject: Very informative TIP

I have been developing in Notes client since R4 and I just found this tip today. It is very helpful.

Subject: Tip: This is how you copy attachments without saving to disk

Anone who has knocked up against the issue of copying attachments has probably reduced it to 2 basic options:

1> create a new doc (usually because the form has computed fields that you need to run) copy any additional fields info and then copy the attachments - by detaching and re-attaching

or

2> create a new doc, copy all items and then remove any unwanted items such as fields and stuff.

The bottom line, is CopyAllItems gets the objects over a lot more efficiently that detaching and re-attaching. So your post is good information for anyone who may be struggling with this right now.

Thanks!