Thursday, February 24, 2011

Copy attachment from one list to another list

This is really interesting stuff because this topic not only covers how to copy attachment(s) from one list to another but also shows parent child relationship between lists.

I had a list and also related sub list, the idea was very simple I had to have one more field in the list which has column called ParentID.

I had attached event handler to the parent list and in the handler I was fetching the assigned to group. So I had to get all users from that group and assign it to each individual, but I also need to track that these all entries are for particular item from parent.

So I wrote a simple handler and in item adding event I copied same list item to all users of Assignedto field from parent in the another list.

All went well, another requirement came in, and if parent is updated then all child items in another list also should be updated. Idea was very simple, but this is tricky. Why tricky? Because if by mistake it is assigned to incorrect group and have to be assigned to two groups at a time, then the best way to do is Get the item id in ItemUpdated event and then fetch all Parent ID from child list and delete all list items and then re insert.

You might think why not to update? Well, I leave up to you. Think about the scenario which I have mentioned above of updating with two or more groups at a time, there can be couple of individuals as well. You need to update all these data. At the end, you will say, yes deleting all list items and reinserting is better. If you feel, updating is better. Leave your comments. I would love to ask you questions. :)

Another requirement came. If item from parent gets deleted, all child items from another list should also be deleted. Again the scenario is very simple, In ItemDeleting event, get the ID of the list item, find all those ParentID items from the child list and then delete those list items from another child list.

Now the challenging part came in, if I add or update and attach multiple attachments, it should also get copied over another list items with Parent ID.

I have already written my code in ItemAdding. I went ahead and wrote a code for getting list item’s attachment. But wait, the main part is you can never have attachments of list item in ItemAdding event because Item has not actually been inserted to the list. So in ItemAdding you can never get attachments.

So the idea was to change the code from ItemAdding to ItemAdded. And guess what, yes I found the attachments there in Item Added event.

o here is a sample code which demonstrate you how to copy list item attachments to another list item.

spWeb.AllowUnsafeUpdates = true;

foreach (string AttachName in EventItem.Attachments)

SPFile oSpFile =
EventItem.ParentList.ParentWeb.GetFile(EventItem.Attachments.UrlPrefix + AttachName);
item.Attachments.Add(AttachName, oSpFile.OpenBinary());



spWeb.AllowUnsafeUpdates = false;

Where EventItem is source list item and item is destination list item.

So it is very simple to copy attachments from one list item to another list item in event handler.


Felipe said...

Nice post man. Thanks a lot :)

Anonymous said...

Thank you

Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to