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

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

  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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