0

How can I add a word document from a document library to a list field ?

I am asking this question because I tried a lot of things (setting a content type supporting documents in a list which doesn't work actually; setting a workflow in SPD without success). What I want to do is simple: everytime a word document is generated in my document library I want to add it to the last item in my list (in the "attachments" field for example) but how .. I am running out of ideas, is there any one who knows how to do it?

  • This feels unnatural from a pure content management perspective...taking a document from a library and making it an attachment on a list item. If there's a need to have the document referenced/accessible from another list why not drop a link to the document in a hyperlink field on the list? – Rob D'Oria May 20 '14 at 11:55
1

I must admit I've tended to go the other way i.e taking attachments and moving them to document libraries so they can be properly managed. However yes you can do this.

Two main approaches to implement this, either a custom workflow action or an event handler on the document library. I personally would always go for the event handler approach.

In your event handler you'll need to first get your file with some code like this:

SPFile docFile= docItem.File;
byte[] fileBinaryData = docFile.OpenBinary();

Then your code the get the last item in your list before you do the following to add the attachment:

listItem.Attachments.Add(docItem.Name, fileBinaryData);
listItem.Update();

Here's a snippet of code I used to test this part to make sure the Attachments command added an attachment to the first item in a list. Please note if you did this before but didn't include the item.Update() then the file is created in the attachments area for the item but not applied to the list item, if you then try and re-add it with the same filename you will get an error.

using (SPSite site = new SPSite("http://sp2013dev/sites/test010/"))
{
    using (SPWeb web = site.RootWeb)
    {
        SPList list = web.Lists["Test List"];
        Byte[] fileBinaryData = Encoding.UTF8.GetBytes("Content of file");
        SPListItem item = list.GetItemById(1);
        item.Attachments.Add("filename.txt", fileBinaryData);
        item.Update();
    }
}
  • Thank you, but can you be more specific with the event handler method – miklow May 20 '14 at 13:50
  • I am new to the event handlers. i dont know how to start can you explain it to me in a more detailed way – miklow May 20 '14 at 14:54
  • I am trying what you've proposed in a sequential workflow as i am familiar with it: the spfile adding and listitem attachment and everything but with no appearance of the file in my list although i ve set a try catch exception to follow errors but now i have no errors and still no file in my list. i don't know why – miklow May 20 '14 at 17:38
  • thank you PristonBlown your snippet of code helped me, with some modifications, to accomplish what it was needed to be done – miklow May 21 '14 at 10:36

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.