Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I want to copy a list item from one list to an other along with the file attachments which are associated with it.

I am doing this in a custom workflow. I am trying to make a object of a class which is coming from Listdata.svc and the class is of TasksItem.

TasksItem newtask = new TasksItem()
{
  Title = workflowProperties.Item.Title,
};

Above is the code which is working fine when copy only simple field like Title of the task etc but I want to know how can we add attachments into it. So the attachments will also get copied to the destination list.

Thanks.

share|improve this question

2 Answers 2

up vote 1 down vote accepted

To copy the attachment You can use the following code:

foreach (string filename in item.Attachments)
{
  var file =  item
               .ParentList
               .ParentWeb
               .GetFile(item.Attachments.UrlPrefix + fileName);
  var imageData = file.OpenBinary();
  newItem.Attachments.Add(fileName, imageData);
}
newItem.Update();
share|improve this answer
newTask.Attachments.Add(attachmentName, attachmentData); 

where attachmentData is a byte-array. You can easily convert attachments to byte[] and add to items.

Check out( warning: error in code sample): http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spattachmentcollection.aspx

share|improve this answer
    
How to get the attached file from "workflowproperties.item", let me know. –  RHM Feb 9 '12 at 12:59
    
I m getting Value cannot be null. Parameter name: args on setsavestream method of datacontext –  RHM Feb 9 '12 at 13:17
    
It should be in workflowproperties.Item.Attachments, no? –  tarjeieo Feb 9 '12 at 13:55
    
yes it is...... –  RHM Feb 9 '12 at 15:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.