0

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.

2 Answers 2

2

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();
0
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

3
  • How to get the attached file from "workflowproperties.item", let me know.
    – RHM
    Commented Feb 9, 2012 at 12:59
  • I m getting Value cannot be null. Parameter name: args on setsavestream method of datacontext
    – RHM
    Commented Feb 9, 2012 at 13:17
  • It should be in workflowproperties.Item.Attachments, no?
    – tarjeieo
    Commented Feb 9, 2012 at 13:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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